Created
March 21, 2018 18:47
-
-
Save Lerbytech/63d1de086c92d809c379b9f7ccf5ef60 to your computer and use it in GitHub Desktop.
заготовка для проверки эффективности realloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <chrono> | |
#include <windows.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fstream> | |
using namespace std; | |
using namespace std::chrono; | |
void function() | |
{ | |
high_resolution_clock::time_point t1; | |
high_resolution_clock::time_point t2; | |
FILE *f; | |
int* many_numbers = NULL; | |
int N = 1; | |
many_numbers = (int*)malloc(N * sizeof(int)); | |
t1 = high_resolution_clock::now(); | |
auto prev = -1; | |
float percent = 0.0f; | |
f = fopen("int.txt", "a"); | |
for (N = 1; N < 1000000; N++) | |
{ | |
many_numbers = (int*)realloc(many_numbers, N * sizeof(int)); | |
//printf("%d \n", many_numbers); | |
if (N % 200000 == 0) | |
{ | |
//cout << N << endl; | |
t2 = high_resolution_clock::now(); | |
auto duration = duration_cast<milliseconds>(t2 - t1).count(); | |
percent = (float)duration / (float)prev; | |
if (prev < 0) | |
prev = duration; | |
cout << N << " " << percent << "x " << " " << duration << " ms" << endl; | |
fprintf(f, "%d %f %d\n", N, percent, duration); | |
t1 = high_resolution_clock::now(); | |
} | |
} | |
fprintf(f, "---\n"); | |
fclose(f); | |
} | |
int main() | |
{ | |
high_resolution_clock::time_point t1 = high_resolution_clock::now(); | |
for(int i = 0; i < 10; i++) | |
function(); | |
high_resolution_clock::time_point t2 = high_resolution_clock::now(); | |
auto duration = duration_cast<microseconds>(t2 - t1).count(); | |
cout << duration << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment