calloc v/s malloc and time efficiency

前端 未结 7 1215
抹茶落季
抹茶落季 2021-02-04 08:12

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.

相关标签:
7条回答
  • 2021-02-04 08:29

    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.

    0 讨论(0)
  • 2021-02-04 08:44

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

    0 讨论(0)
  • 2021-02-04 08:46

    The calloc and memset 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.

    0 讨论(0)
  • 2021-02-04 08:47

    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() or bzero() (that are called by calloc() 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.

    0 讨论(0)
  • 2021-02-04 08:52

    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.

    0 讨论(0)
  • 2021-02-04 08:52

    malloc differ by calloc by two reason

    1. malloc takes one argument whereas calloc takes two argument

    2. 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

    0 讨论(0)
提交回复
热议问题