20

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?

5
  • possible duplicate of stackoverflow.com/questions/1538420/… Commented Apr 9, 2010 at 6:36
  • Yes, this is why I mentioned that I read the other post. I wanted to be a bit more specific here. Commented Apr 9, 2010 at 6:57
  • Have you measured it yourself? What were the results on your machine? Commented Apr 9, 2010 at 7:05
  • Not yet. I'll be back to write if there are differences. Commented Apr 9, 2010 at 7:09
  • 1
    Here's a related (later) question which gets into why calloc can be much faster (10x) than malloc+memset. stackoverflow.com/questions/2688466 Commented Apr 22, 2010 at 6:45

7 Answers 7

21

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

Sign up to request clarification or add additional context in comments.

11 Comments

Great. A question: Is memset() causes initialization to zero or NULL?
I'm using C. I think memset() is for C++ and is not available in C.
@yCa: memset is available in both C++ and C. NULL is a constant designed to be used to initialize pointers, and using it for other things (such as when an int value is desired) is wrong, even if it might work.
I would not dare to state that a malloc/memset sequence is as fast as calloc. This depends on the libc implementation. What if memory provided by the OS is already initialized to zero? The calloc implementation may know about this and thus skip zeroing the memory. The memset in a malloc/memset sequence would be redundant and certainly not as fast as calloc. For example under Linux, the mmap() system call is used when large memory chunks are requested and memory is already zeroed by Linux.
@Fabian: The memory is not "already zeroed" at the time of mmap. Instead it's pure untouched copy-on-write references to the universal zero page. It will be instantiated as physical memory filled with zeros on the first write. So using calloc defers the cost of zero-initializing memory from allocation time to first-write time. This can be very useful in realtime applications where a single large memset could result in too much latency, but the cost spread out over many subsequent local accesses is acceptable.
|
3

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.

Comments

1

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.

1 Comment

Thanks for your interesting input.
0

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.

1 Comment

Thanks. What's happening inside the loop can be much more time consuming.
0

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

Comments

0

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.

Comments

-9

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.