-
Speed test result
Here is speed test result for malloc, realloc and calloc at Dual Core @1.86Ghz, 1.75 GB RAM...
Code:
malloc + free:
Pass 1: 1359 ms
Pass 2: 1391 ms
Pass 3: 1359 ms
Pass 4: 1344 ms
Pass 5: 1359 ms
Pass 6: 1328 ms
Pass 7: 1344 ms
Pass 8: 1344 ms
Pass 9: 1375 ms
Pass 10: 1359 ms
Average: 1356.20 ms
realloc(NULL) + free: //similar to malloc
Pass 1: 1344 ms
Pass 2: 1406 ms
Pass 3: 1328 ms
Pass 4: 1329 ms
Pass 5: 1343 ms
Pass 6: 1344 ms
Pass 7: 1328 ms
Pass 8: 1344 ms
Pass 9: 1328 ms
Pass 10: 1328 ms
Average: 1342.20 ms
calloc + free:
Pass 1: 4734 ms
Pass 2: 4672 ms
Pass 3: 4641 ms
Pass 4: 4609 ms
Pass 5: 4641 ms
Pass 6: 4672 ms
Pass 7: 4672 ms
Pass 8: 4640 ms
Pass 9: 4625 ms
Pass 10: 4625 ms
Average: 4653.10 ms
realloc:
Pass 1: 656 ms
Pass 2: 672 ms
Pass 3: 656 ms
Pass 4: 735 ms
Pass 5: 671 ms
Pass 6: 688 ms
Pass 7: 656 ms
Pass 8: 672 ms
Pass 9: 656 ms
Pass 10: 672 ms
Average: 673.40 ms
Source-
Code:
int *test = NULL; //int sequence
DWORD start = 0;
unsigned int i, j, total = 0;
for(i=1; i<=10; i++)
{
start = GetTickCount();
for(j=1; j<=320000; j++)
{
/*
test = calloc(j, sizeof(int));
free(test);
*/
/*
test = malloc(j * sizeof(int));
free(test);
*/
/*
test = realloc(NULL, j * sizeof(int)); //same as malloc
free(test);
*/
/*
test = malloc(j * sizeof(int));
free(test);
*/
/*
test = realloc(test, j * sizeof(int));
*/
}
total += (GetTickCount() - start);
printf("Pass %d: %d ms\n", i, (unsigned int)(GetTickCount() - start));
}
//free(test); //for realloc(test, j* sizeof(int));
printf("Average: %.2f ms\n", (float)total / (float)10);
return 0;
Is there portable version of GetTickCount? I tried _ftime.millis but it show annoying result (negative numbers)...
-
-
I'm not sure what, precisely, GetTickCount() does, but based on a guess, there is a standard function called clock() which is portable. How effective it is on a particular system, of course, varies...
By the bye, one reason you may get negative numbers is that you're using %d in printf(). %d means signed int. If you're passing an unsigned value (such as in your code when you cast), it will interpret it as signed and possibly print out as a negative number.
Anyhow, here's a version of your program using clock():
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int *test;
clock_t start, total = 0;
long i, j;
for(i = 0; i < 10; i++)
{
start = clock();
for(j = 0; j < 320000; j++)
{
/*
test = calloc(j, sizeof(int));
free(test);
*/
/*
test = malloc(j * sizeof(int));
free(test);
*/
/*
test = realloc(NULL, j * sizeof(int)); //same as malloc
free(test);
*/
/*
test = malloc(j * sizeof(int));
free(test);
*/
/*
test = realloc(test, j * sizeof(int));
*/
}
total += clock() - start;
printf("Pass %ld: %.2f s\n", i + 1, (total - start) / (double)CLOCKS_PER_SEC);
}
//free(test); //for realloc(test, j* sizeof(int));
printf("Average: %.2f s\n", (total / 10.0) / CLOCKS_PER_SEC);
return 0;
}
-
Thanks cas, that very helpful ^_^!
-
Also, it would probably be a good idea to test free and malloc separately (so, allocate 32k blocks of memory, then free all of them in a separately timed sequence).
Your test also shows that realloc probably doesn't actually copy the content each time - it would be slower than calloc() if it did.
--
Mats