一些调用“calloc”是可疑的快速 [英] Some calls to "calloc" are suspiciously fast

查看:218
本文介绍了一些调用“calloc”是可疑的快速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在分配内存时:

  point_1 = calloc(100000000,16); //这需要将近1秒,并且perf从CACHE-> RAM 


$ b从RAM-> CACHE和1M发现27M传输$ b

这是好的。



但是当试图分配两个数组时:

  point_1 = calloc(100000000,16); 
point_2 = calloc(100000000,16);
//再次,程序需要将近1秒,27M传输RAM-CACAHE,1M CACHE-> RAM

看起来,第二个calloc(以及后面的所有)的行为就像malloc。我正在使用gcc版本4.9.2(Ubuntu 4.9.2-0ubuntu1〜12.04)
。否则,程序工作正常。



行为正常吗?



以下是一些测试和结果: p>

 分配数据结构1的时间:0.976468 
Perf:R:27M,W:1M

分配数据结构的时间1:0.975402
数据结构1初始化为7的值的时间:0.296787
Perf:R:52M,W:26M

时间用于分配数据结构1:0.976034
数据结构1初始化为7的值的时间:0.313554
分配数据结构2的时间:0.000031< - 行为不正常
Perf:R :52M,W:26M

分配数据结构1的时间:0.975403
数据结构1初始化为7的值的时间:0.313710
分配数据结构的时间2:0.000031 < - 行为不当
数据结构2初始化为7的值的时间:0.809855
Perf:R:79M,W:53M


解决方

它可以证实,第二释放calloc需要更短的时间。看来,Linux决定推迟一些实际工作。



在我的系统中,第一个calloc需要大约0.7秒。
如果我然后遍历分配的内存区域,将其设置为非零值,则需要0.2秒。总共0.9秒。



第二个calloc需要0.0秒,但设置第二个区域需要0.9秒。同样的总时间,但看起来像Karoly Horvath在评论中写道的第二个calloc实际上并没有创建内存页面,但是在访问内存时会导致页面错误。



Karoly Horvath的另一个很棒的评论与这个相关的问题有关:为什么malloc + memset比calloc慢?



测试Ubuntu 14.04.1 LTS在Intel Core i7-4790K上运行,带有-O2和a GCC自称gcc(Ubuntu 4.8.2-19ubuntu1)4.8.2。 Glibc版本是Ubuntu EGLIBC 2.19-0ubuntu6.4。


I'm benchmarking with "Perf" (Linux, gcc).

When allocating memory:

point_1 = calloc (100000000, 16);  //this takes nearly 1 second and perf find 27M transfers from RAM->CACHE and 1M from CACHE->RAM 

This is OK.

But when trying to allocate two arrays:

point_1 = calloc (100000000, 16); 
point_2 = calloc (100000000, 16);
  //again, program takes nearly 1 second, 27M transfers RAM-CACAHE, 1M CACHE->RAM

It looks like, that second "calloc" (and all following) are behave like "malloc". I'm using "gcc version 4.9.2 (Ubuntu 4.9.2-0ubuntu1~12.04) ". Otherwise program works fine.

Is that behaving OK?

Here are some more tests and results:

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

解决方案

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.

这篇关于一些调用“calloc”是可疑的快速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆