我将尝试理解
这个程序的输出如下: 这是我得到的输出:
我很惊讶当向量的大小增加时,
malloc
和free
的时间关系。因此,我编写了这个简单的程序:#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);
/* I do this to avoid lazy allocation */
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;
}
这个程序的输出如下: 这是我得到的输出:
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)
我很惊讶当向量的大小增加时,
malloc
的花费是如此之低。随着大小的增加,它不应该急剧增加吗?我的第二个问题是关于free
函数。我一直认为malloc
比free
昂贵,而这不合理。对于我来说,它更昂贵了。我对系统如何处理内存(物理页和虚拟页)有一些了解,但这些结果对我来说没有意义。malloc
并不那么昂贵...或者是吗?:)
欢迎任何评论!
编辑:感谢所有快速评论!非常感谢!我考虑了评论,并稍微改变了我的程序。我使用了calloc代替了malloc。此外,我两次穿过数组,并计算了两次时间。第一次是为了确保分配了所有页面,第二次是为了测试仅访问数组的时间。显然,有一个随着数组大小增加而增加的差异!
我正在尝试获得我的算法的一些性能结果,因此我希望消除这个额外的成本。我的算法中使用的大多数内存都在开始时分配。 有没有办法告诉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)
calloc()
更符合您的期望,因为它初始化内存。 - Ctx