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()
.
yCalleecharan
Updated on May 27, 2020Comments
-
yCalleecharan almost 4 years
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?
-
yCalleecharan about 14 yearsGreat. A question: Is memset() causes initialization to zero or NULL?
-
yCalleecharan about 14 yearsI'm using C. I think memset() is for C++ and is not available in C.
-
Admin about 14 years@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.
-
yCalleecharan about 14 yearsThanks for the clarifications. I looked in my C book and didn't find memset() :). But I shall investigate this memset() if I'll have to use it.
-
Secure about 14 yearsGo and buy a better book. Or simply enter "c memset" in Google. Or read in Wikipedia, e.g. here: en.wikipedia.org/wiki/C_standard_library. I mean, if you want to seriously program in C, and I assume that you want if you care about such subtle performance issues (premature optimization?), then you're well advised to learn what the standard library already provides.
-
yCalleecharan about 14 yearsYes, the web has lot of info. I really need to get better books though. By the way, have you heard about the book: The Standard C Library by Plauger? It came out in 1991. I see it does have memset :). Do u think that a 1991 book on standard libraries is still good to read? C hasn't changed much since then.
-
yCalleecharan about 14 yearsThanks for your interesting input.
-
yCalleecharan about 14 yearsThanks. What's happening inside the loop can be much more time consuming.
-
Fabian over 13 yearsI 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.
-
R.. GitHub STOP HELPING ICE almost 13 years@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 usingcalloc
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 largememset
could result in too much latency, but the cost spread out over many subsequent local accesses is acceptable. -
Fabian almost 13 years@R..: Interesting, didn't know there was a universal zero page. But that still means that calloc is faster than malloc/memset. Because on the first write in memset, the entire page will be zeroed, and the following writes of memset is a waste of cpu cycles/memory bandwidth. Or am I missing something?
-
R.. GitHub STOP HELPING ICE almost 13 yearsThat's also right, but not a huge issue. The page fault to instantiate the physical page costs several times as much as a 4k
memset
. -
Dilawar about 10 yearsAllocating memory using
calloc
is faster on my system. github.com/dilawar/MyPublic/blob/master/CLike/malloc_calloc.c