Some calls to “calloc” are suspiciously fast [duplicate]

穿精又带淫゛_ 提交于 2019-12-05 23:18:56
Thomas Padron-McCarthy

It can confirm that the second calloc takes much shorter time. It seems that Linux decides to postpone some of the actual work.

On my system, the first calloc takes around 0.7 seconds. If I then iterate over the allocated memory area, setting it to something other than zero, this takes 0.2 seconds. In total, 0.9 seconds.

The second calloc then takes 0.0 seconds, but setting the second area takes 0.9 seconds. Same total time, but it seems that the second calloc, as Karoly Horvath wrote in a comment, doesn't actually create the memory pages, but leaves that to page faults when accessing the memory.

Another great comment by Karoly Horvath linked to this related question: Why malloc+memset is slower than calloc?

Tested on Ubuntu 14.04.1 LTS running on an Intel Core i7-4790K, with -O2 and a GCC that calls itself "gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2". Glibc version is Ubuntu EGLIBC 2.19-0ubuntu6.4.

Each of the calls tries to allocate 1.6 GB of memory. I suspect the second call is failing, which would explain the symptoms. Check the return value from calloc().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!