释放calloc V / S malloc和时间效率 [英] calloc v/s malloc and time efficiency

查看:263
本文介绍了释放calloc V / S malloc和时间效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我饶有兴趣地阅读后 malloc和释放calloc C之间的区别。我用我的code malloc和想知道我将不得不使用释放calloc,而不是什么区别。

我的present(伪)code使用malloc:

情景1

  INT的main()
{
   分配使用malloc大阵列   初始化所有的数组元素ZERO   循环//说1000次
    做什么,结果写入阵列
   结束循环   FREE阵列,free命令} //主到底

如果我用释放calloc代替malloc的,那么我将有:

Scenario2

  INT的main()
{   循环//说1000次
    分配用calloc阵列    做什么,结果写入阵列    FREE阵列,free命令   结束循环
} //主到底

我有三个问题:


  1. 其中的情景更有效,如果阵列是非常大的?


  2. 其中的情景会更加高效的时间如果阵列是非常大的?


  3. 在这两种情况下,我只是写在某种意义上说,在任何给定的迭代循环,我在写每个数组按顺序从第一个元素到最后一个元素的数组。重要的问题:如果我在方案1中使用malloc的,那么是否有必要,我初始化元素为零?使用malloc说我有数组z = [garbage1,garbage2,垃圾3]。对于每次迭代,我在第一次迭代,我得到Z = [some_result,garbage2,garbage3]按顺序写,即元素,在第二次迭代,我得到了第一次迭代中,我得到Z = [some_result,another_result,garbage3]等,那么我是否需要专门的malloc后,初始化数组我?



解决方案

假设内存总量在你的两个例子被初始化是一样的,分配与)的内存释放calloc(可能比记忆与的malloc(分配),然后归零出来在一个单独的步骤更快,尤其是如果在的malloc() 如果你在一个循环遍历它们分别为零的元素。 A 的malloc()后跟一个 memset的()将可能是一样快释放calloc ()

如果你不介意的数组元素都是垃圾之前,你实际存储在他们的计算结果,也没有必要在实际初始化数组的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:

  1. Which of the scenarios is more efficient if the arrays are very large?

  2. Which of the scenarios will be more time efficient if the arrays are very large?

  3. 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?

解决方案

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 with malloc() and then zeroing them out in a separate step, especially if in the malloc() case you zero the elements individually by iterating over them in a loop. A malloc() followed by a memset() will likely be about as fast as calloc().

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().

这篇关于释放calloc V / S malloc和时间效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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