一些对“calloc”的调用非常快
作者:互联网
参见英文答案 > Why malloc+memset is slower than calloc? 3个
我正在使用“Perf”(Linux,gcc)进行基准测试.
分配内存时:
point_1 = calloc (100000000, 16); //this takes nearly 1 second and perf find 27M transfers from RAM->CACHE and 1M from CACHE->RAM
还行吧.
但是在尝试分配两个数组时:
point_1 = calloc (100000000, 16);
point_2 = calloc (100000000, 16);
//again, program takes nearly 1 second, 27M transfers RAM-CACAHE, 1M CACHE->RAM
看起来,第二个“calloc”(以及所有后续内容)的行为类似于“malloc”.我正在使用“gcc版本4.9.2(Ubuntu 4.9.2-0ubuntu1~12.04)
”.其他程序工作正常.
那表现不错吗?
以下是一些测试和结果:
Time for allocating of data structure 1: 0.976468
Perf: R:27M, W:1M
Time for allocating of data structure 1: 0.975402
Time for initialization of data structure 1 to value of 7: 0.296787
Perf: R: 52M, W: 26M
Time for allocating of data structure 1: 0.976034
Time for initialization of data structure 1 to value of 7: 0.313554
Time for allocating of data structure 2: 0.000031 <-- misbehaving
Perf: R: 52M, W:26M
Time for allocating of data structure 1: 0.975403
Time for initialization of data structure 1 to value of 7: 0.313710
Time for allocating of data structure 2: 0.000031 <-- misbehaving
Time for initialization of data structure 2 to value of 7: 0.809855
Perf: R:79M, W: 53M
解决方法:
它可以确认第二个calloc需要更短的时间.似乎Linux决定推迟一些实际工作.
在我的系统上,第一个calloc需要大约0.7秒.
如果我然后迭代分配的内存区域,将其设置为非零值,则需要0.2秒.总共0.9秒.
然后第二个calloc需要0.0秒,但设置第二个区域需要0.9秒.相同的总时间,但似乎第二个calloc,正如Karoly Horvath在评论中写的那样,实际上并没有创建内存页面,而是在访问内存时留下了页面错误.
Karoly Horvath的另一个伟大评论与此相关问题有关:Why malloc+memset is slower than calloc?
测试在英特尔酷睿i7-4790K上运行的Ubuntu 14.04.1 LTS,其中-O2和GCC称自己为“gcc(Ubuntu 4.8.2-19ubuntu1)4.8.2”. Glibc版本是Ubuntu EGLIBC 2.19-0ubuntu6.4.
标签:c-3,calloc,linux,memory,gcc 来源: https://codeday.me/bug/20190830/1766257.html