calloc v/s malloc and time efficiency

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


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.