2

Lets say, I have a function (or functions) which takes a long time (wall time) to execute, for example:

#include "stdafx.h" #include <math.h> #include <windows.h> void fun() { long sum = 0L; for (long long i = 1; i < 10000000; i++){ sum += log((double)i); } } double cputimer() { FILETIME createTime; FILETIME exitTime; FILETIME kernelTime; FILETIME userTime; if ( GetProcessTimes( GetCurrentProcess( ), &createTime, &exitTime, &kernelTime, &userTime ) != -1 ) { SYSTEMTIME userSystemTime; if ( FileTimeToSystemTime( &userTime, &userSystemTime ) != -1 ) return (double)userSystemTime.wHour * 3600.0 + (double)userSystemTime.wMinute * 60.0 + (double)userSystemTime.wSecond + (double)userSystemTime.wMilliseconds / 1000.0; } } int _tmain(int argc, _TCHAR* argv[]) { double start, stop; start = cputimer(); fun(); stop = cputimer(); printf("Time taken: %f [seconds]\n", stop - start); return 0; } 

I would like to measure a CPU load of this function and RAM usage that this function call uses. Is that possible? How can I do this? Im interested in Windows and Linux solutions.

2 Answers 2

2

On POSIX you can try using getrusage in the manner similar to how you check the wall time. Not sure about windows.

Sign up to request clarification or add additional context in comments.

4 Comments

yup I know getrusage - I used to measure CPU time. But how about CPU load?
There's no such thing as cpu load of function. It is metric pertaining to cpu.
So CPU load can be obtained for a process only, not a function itself? How about checking CPU load before and after function call and do the subtraction after?
I think cpu time delta is the metric you're looking for. After all it's about how much cpu your function uses.
1

There is GetProcessTimes function for windows which can give you the CPU time. Also check the Process Status API

Also there is SIGAR which is platform independent.

On Linux you can try with getrusage

17 Comments

But it all will give me memory per process, not per function call, right?
Memory is not allocated to function. You can either compare before and after or you can link with your own alloc.
@MichaelKrelin-hacker:- Very correct! Was about to write the same :)
@Michael Krelin - hacker: OK, I see. What do you mean by 'my own alloc'?
@RahulTripathi, which comes down to cpu time since number of cores is too constant and wall time is too variable ;)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.