calloc 与 malloc 和时间效率
我感兴趣地阅读了这篇文章 malloc 和 calloc 之间的 C 区别。我在代码中使用 malloc,想知道使用 calloc 会有什么区别。
我现在使用 malloc 的(伪)代码:
场景 1
int main()
{
allocate large arrays with malloc
INITIALIZE ALL ARRAY ELEMENTS TO ZERO
for loop //say 1000 times
do something and write results to arrays
end for loop
FREE ARRAYS with free command
} //end main
如果我使用 calloc 而不是 malloc,那么我将得到:
场景 2
int main()
{
for loop //say 1000 times
ALLOCATION OF ARRAYS WITH CALLOC
do something and write results to arrays
FREE ARRAYS with free command
end for loop
} //end main
我有三个问题:
哪一个如果数组很大,哪种场景效率更高?
如果数组非常大,哪些场景会更省时?
在这两种情况下,我只是写入数组,对于 for 循环中的任何给定迭代,我都从第一个元素到最后一个元素按顺序写入每个数组。重要的问题是:如果我像场景 1 一样使用 malloc,那么是否有必要将元素初始化为零?假设使用 malloc 我有数组 z = [garbage1,garbage2,garbage3]。对于每次迭代,我按顺序写入元素,即在第一次迭代中我得到 z =[some_result,garbage2,garbage3],在第二次迭代中我在第一次迭代中得到 z =[some_result,another_result,garbage3] 等等on,那么我是否需要在 malloc 之后专门初始化数组?
I've read with interest the post C difference between malloc and calloc. I'm using malloc in my code and would like to know what difference I'll have using calloc instead.
My present (pseudo)code with malloc:
Scenario 1
int main()
{
allocate large arrays with malloc
INITIALIZE ALL ARRAY ELEMENTS TO ZERO
for loop //say 1000 times
do something and write results to arrays
end for loop
FREE ARRAYS with free command
} //end main
If I use calloc instead of malloc, then I'll have:
Scenario2
int main()
{
for loop //say 1000 times
ALLOCATION OF ARRAYS WITH CALLOC
do something and write results to arrays
FREE ARRAYS with free command
end for loop
} //end main
I have three questions:
Which of the scenarios is more efficient if the arrays are very large?
Which of the scenarios will be more time efficient if the arrays are very large?
In both scenarios,I'm just writing to arrays in the sense that for any given iteration in the for loop, I'm writing each array sequentially from the first element to the last element. The important question: If I'm using malloc as in scenario 1, then is it necessary that I initialize the elements to zero? Say with malloc I have array z = [garbage1, garbage2, garbage 3]. For each iteration, I'm writing elements sequentially i.e. in the first iteration I get z =[some_result, garbage2, garbage3], in the second iteration I get in the first iteration I get z =[some_result, another_result, garbage3] and so on, then do I need specifically to initialize my arrays after malloc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
假设两个示例中初始化的内存总量相同,则使用
calloc()
分配内存可能比使用malloc()
分配内存更快,然后在单独的步骤中将它们清零,特别是在malloc()
情况下,您通过在循环中迭代它们来单独将元素清零。malloc()
后跟memset()
可能与calloc()
一样快。如果您在实际将计算结果存储到数组元素之前并不关心数组元素是否是垃圾,则无需在
malloc()
之后实际初始化数组。Assuming the total amount of memory being initialized in your two examples is the same, allocating the memory with
calloc()
might be faster than allocating the memory withmalloc()
and then zeroing them out in a separate step, especially if in themalloc()
case you zero the elements individually by iterating over them in a loop. Amalloc()
followed by amemset()
will likely be about as fast ascalloc()
.If you do not care that the array elements are garbage before you actually store the computation results in them, there is no need to actually initialize your arrays after
malloc()
.对于 1 和 2,两者都执行相同的操作:分配和清零,然后使用数组。
对于 3,如果不需要先将数组清零,则不需要清零,而且不这样做会更快。
calloc 的清零可能比您编写的代码更有效,但与程序执行的其余工作相比,这种差异很小。 calloc 真正节省的地方是不必自己编写该代码。
For 1 and 2, both do the same thing: allocate and zero, then use the arrays.
For 3, if you don't need to zero the arrays first, then zeroing is unnecessary and not doing it is faster.
There is a possibility that calloc's zeroing is more efficient than the code you write, but this difference will be small compared to the rest of the work the program does. The real savings of calloc is not having to write that code yourself.
您在 3. 中所述的观点似乎表明了一种情况或不必要的初始化。这在速度方面是相当糟糕的,不仅浪费了执行此操作的时间,而且还因此发生了大量缓存驱逐。
执行
memset()
或bzero()
(无论如何都会由calloc()
调用)是使大部分内容无效的好方法。缓存。除非您确定不会覆盖所有内容,但可以读取尚未写入的缓冲区部分(就好像 0 是可接受的默认值一样),否则不要这样做。如果你无论如何都要重写所有内容,那么请不要不必要地初始化你的内存。不必要的内存写入不仅会破坏您的应用程序性能,还会破坏与之共享同一 CPU 的所有应用程序的性能。
Your point stated in 3. seems to indicate a case or unnecessary initialization. That is pretty bad speed wise, not only the time spent doing it is wasted but a whole lot of cache eviction happened because of it.
Doing a
memset()
orbzero()
(that are called bycalloc()
anyway) is a good way to invalidate huge portion of your cache. Don't do it unless you are sure you won't overwrite everything yet can read parts of the buffer that will not have been written (as if 0 is an acceptable default value). If you write over everything anyway by all mean don't initialize your memory unnecessarily.Unnecessary memory writing will not only ruin your app performance but also the performance of all applications sharing the same CPU with it.
calloc
和memset
方法应该大致相同,并且可能比自己将其归零稍快一些。无论如何,这都与您在主循环中所做的事情相关,这可能要大几个数量级。
The
calloc
andmemset
approaches should be about the same, and maybe slightly faster than zeroing it yourself.Regardless, it's all relative to what you do inside your main loop, which could be orders of magnitude larger.
malloc 比 Calloc 更快,因为 malloc 返回来自操作系统的内存。但是,当您调用 Calloc 时,它会从内核或操作系统获取内存,并用零进行初始化,然后返回给您。
所以,初始化需要时间。这就是为什么 malloc 比 Calloc 快
malloc is faster than Calloc because the reason is that malloc return memory as it is from an operating system. But when you will call Calloc it gets memory from the kernel or operating system and its initializes with its zero and then its return to you.
so, the initialization takes time. that's why malloc faster than Calloc
我不知道linux。但在 Windows 上有一种称为零页线程的东西... calloc 使用那些已经初始化为零的页面。 malloc 和 calloc 的速度没有区别。
I dont know for linux. But on Windows there is something called the zero-page thread... calloc use those pages already initialized to zero. There is no difference in speed between malloc and calloc.
malloc
与calloc
的区别有两个原因malloc 采用一个参数,而 calloc 采用两个参数
malloc比 calloc 快的原因是 malloc 将单维数组处理为指针格式,而 calloc 接受二维数组,在处理之前将其转换为单维数组,然后转换为指针格式。
我认为这就是为什么 malloc 处理速度比 calloc 更快
malloc
differ bycalloc
by two reasonmalloc takes one argument whereas calloc takes two argument
malloc is faster than calloc reason is that malloc processed single dimensional array to pointer format whereas calloc takes double dimensional array and before processed it converts to single dimensional array then to pointer format.
I think that, that's why malloc processing faster as compared to calloc