malloc and new functions

F

friendrangar

Hello

Which function is faster , malloc or new ? why?

How much CPU utilisation will require for both and how to calculate CPU
utilisation for any function ?
 
A

Axter

Hello

Which function is faster , malloc or new ? why?

How much CPU utilisation will require for both and how to calculate CPU
utilisation for any function ?


You're comparing apples to oranges. malloc and new have different
functionallities.
malloc allocates memory.
Where as new allocates memory, and calls the object's constuctor.


---------------------------------------------------------------------------­­-------------

David Maisonave
http://axter.com


Author of policy smart pointers (http://axter.com/smartptr)
---------------------------------------------------------------------------­­-------------
 
S

Sunil Varma

Hello

Which function is faster , malloc or new ? why?

How much CPU utilisation will require for both and how to calculate CPU
utilisation for any function ?

new is an operator where as malloc() is a function.
Probably malloc() is faster, because it doesn't call the constructor,
where as new does.
Always use new to allocate memory dynamically.

I dont think there is a function that returns the CPU usage by a
funciton in C++.

But you can find out how much time a function took for execution.

int before = clock();
function(); // Your function here.
int after = clock();

now (after-before) gives you the time consumed by the function
function().

Regards
Sunil
 
A

asterisc

now (after-before) gives you the time consumed by the function
function().
The returned time is the time consumed by Operating System and the
function, not just by the function itself.
If you have many processes in memory, and a lot of threads, the time
obtained will be usefull.
You can run the test for milion times, then take the average value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top