Question

What I want to know is that if I use calloc/malloc in a c++ program instead of operator new, does it make the memory allocation faster or it hardly matters as c++ compiler is being used to compile the program.

Edit:

I suppose it should have been taken as obvious that I am not using new operator to call the constructor. Just memory allocation like for an array.

Was it helpful?

Solution

With most compilers I've tested, the extra initialization carried out when you use new means that it's minutely slower than malloc (at least when dealing with simple types for which the two are at least vaguely comparable). For example:

Test Name:   D000001                         Class Name:  Allocation
CPU Time:        56.8  nanoseconds           plus or minus       2.84
Wall/CPU:        1.02  ratio.                Iteration Count:  419430400
Test Description:
 Dynamic array allocation, use and deallocation time measurement
 Dynamic array of 1000 integers
 get space on heap using malloc() and use it in a procedure on each call



Test Name:   D000002                         Class Name:  Allocation
CPU Time:         238  nanoseconds           plus or minus       11.9
Wall/CPU:        1.03  ratio.                Iteration Count:  104857600
Test Description:
 Dynamic array allocation, initialization, use and deallocation time measurement

 Dynamic array of 1000 integers
 get space on heap using malloc() and use it in a procedure on each call



Test Name:   D000003                         Class Name:  Allocation
CPU Time:        60.4  nanoseconds           plus or minus       3.02
Wall/CPU:        1.02  ratio.                Iteration Count:  419430400
Test Description:
 Dynamic array allocation, use and deallocation time measurement
 Dynamic array of 1000 integers
 get space on heap using NEW and use it in a procedure on each call



Test Name:   D000004                         Class Name:  Allocation
CPU Time:         249  nanoseconds           plus or minus       12.4
Wall/CPU:        1.03  ratio.                Iteration Count:  104857600
Test Description:
 Dynamic array allocation, initialization, use and deallocation time measurement

 Dynamic array of 1000 integers
 get space on heap using NEW and use it in a procedure on each call

So, malloc is faster on average, but there's enough variation in speed (in both new and malloc) that an individual invocation of new might actually be faster than an individual invocation of malloc.

OTHER TIPS

You're comparing apples and oranges. malloc() and calloc() allocate memory. new allocates memory, via a possibly over-ridden operator, and calls a constructor. They do different things. Comparing them isn't valid. The fact that 'a C++ compiler is being used to compile the program' is (a) obvious and (b) irrelevant.

malloc does the equivalent task of operator new in c++, not new operator. They just allocates a memory location large enough for your need. new operator additionally fills the allocated memory with proper data by creating an object by calling the constructor in that memory. calloc fills in the bit with zeroes.

Which one of malloc/calloc and operator new is more efficient? This is implementation dependent. Both allocates memory of certain size of returns as void*.

new would normally be implemented as a wrapper that just calls malloc. The difference is the C++ semantics that it adds (calling constructors, handling exceptions etc).

Like any performance question about C++, it depends on your compiler. But any widely-used compiler will optimize new to be as fast as malloc. Write a test program and see for yourself.

(I'm assuming you mean something like new char[100], rather than constructing an object)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top