I try to understand the timings mallocand free. So I wrote this simple program:
#include <stdlib.h>
#include <stdio.h>
int
main()
{
long i;
for(i = 2; i < 10000000000; i*=2) {
struct timeval start, end;
double timing ;
long j;
gettimeofday(&start, NULL);
double *vect = malloc((size_t) i * sizeof(*vect));
if (!vect) {
printf("malloc failed\n");
exit(-1);
}
gettimeofday(&end, NULL);
timing = (double) (end.tv_sec * 1e6 + end.tv_usec) - (start.tv_sec * 1e6 + start.tv_usec);
printf("size %ld allocating (%f)\t", i * sizeof(*vect), timing);
for(j = 0; j < i; j++)
vect[i] = 2;
gettimeofday(&start, NULL);
free(vect);
gettimeofday(&end, NULL);
timing = (double) (end.tv_sec * 1e6 + end.tv_usec) - (start.tv_sec * 1e6 + start.tv_usec);
printf("deallocating (%f)\n", timing);
}
return 0;
}
The result of this program looks like This is what I get as output:
size 16 allocating (40.000000) deallocating (0.000000)
size 32 allocating (0.000000) deallocating (0.000000)
size 64 allocating (0.000000) deallocating (0.000000)
size 128 allocating (0.000000) deallocating (1.000000)
size 256 allocating (0.000000) deallocating (0.000000)
size 512 allocating (0.000000) deallocating (0.000000)
size 1024 allocating (1.000000) deallocating (0.000000)
size 2048 allocating (0.000000) deallocating (0.000000)
size 4096 allocating (1.000000) deallocating (0.000000)
size 8192 allocating (1.000000) deallocating (0.000000)
size 16384 allocating (1.000000) deallocating (0.000000)
size 32768 allocating (1.000000) deallocating (1.000000)
size 65536 allocating (1.000000) deallocating (0.000000)
size 131072 allocating (1.000000) deallocating (1.000000)
size 262144 allocating (2.000000) deallocating (4.000000)
size 524288 allocating (2.000000) deallocating (2.000000)
size 1048576 allocating (1.000000) deallocating (2.000000)
size 2097152 allocating (3.000000) deallocating (3.000000)
size 4194304 allocating (2.000000) deallocating (4.000000)
size 8388608 allocating (4.000000) deallocating (3.000000)
size 16777216 allocating (2.000000) deallocating (3.000000)
size 33554432 allocating (3.000000) deallocating (2.000000)
size 67108864 allocating (2.000000) deallocating (7.000000)
size 134217728 allocating (7.000000) deallocating (8.000000)
size 268435456 allocating (6.000000) deallocating (8.000000)
size 536870912 allocating (5.000000) deallocating (10.000000)
size 1073741824 allocating (6.000000) deallocating (12.000000)
size 2147483648 allocating (25.000000) deallocating (13.000000)
size 4294967296 allocating (7.000000) deallocating (11.000000)
size 8589934592 allocating (6.000000) deallocating (11.000000)
I am really surprised how cheap mallocit is when the size of a vector increases. Shouldn't it increase more dramatically with size?
And my second question is about function free. I always thought it mallocwas expensive, not free. This is more expensive, which for me does not make sense.
I have some knowledge about how the system processes memory (physical pages and virtual pages), but these results make sense to me. mallocnot so expensive ... or is it? :)
Any comments are welcome!
:
! ! , . malloc calloc. , , . , , , - . , !
, . , , . malloc ?. , ( :)) .
size 262144 allocating (5.000000) first pass (166.000000) second pass (190.000000) diff between passes (24.000000) deallocating (10.000000)
size 524288 allocating (4.000000) first pass (330.000000) second pass (328.000000) diff between passes (2.000000) deallocating (3.000000)
size 1048576 allocating (2.000000) first pass (669.000000) second pass (673.000000) diff between passes (4.000000) deallocating (5.000000)
size 2097152 allocating (5.000000) first pass (1326.000000) second pass (1314.000000) diff between passes (12.000000) deallocating (6.000000)
size 4194304 allocating (4.000000) first pass (2655.000000) second pass (2586.000000) diff between passes (69.000000) deallocating (5.000000)
size 8388608 allocating (4.000000) first pass (4858.000000) second pass (4838.000000) diff between passes (20.000000) deallocating (5.000000)
size 16777216 allocating (3.000000) first pass (9034.000000) second pass (8458.000000) diff between passes (576.000000) deallocating (4.000000)
size 33554432 allocating (3.000000) first pass (15702.000000) second pass (14375.000000) diff between passes (1327.000000) deallocating (4.000000)
size 67108864 allocating (4.000000) first pass (25785.000000) second pass (23228.000000) diff between passes (2557.000000) deallocating (3.000000)