Are calloc/malloc faster than operator new in C++
c++, dynamic-memory-allocation
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`.
Problem
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.