PRVS
PRVS

Reputation: 1690

Measure CPU/memory consumption

I used this for measure the time execution in c++:

struct timeval t1, t2;
gettimeofday(&t1, NULL);
gettimeofday(&t2, NULL);
int milliSeconds = (t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec)/1000;

But I want to measure also the cpu / memory consumption. How can I do this?

Upvotes: 1

Views: 668

Answers (1)

Liran Funaro
Liran Funaro

Reputation: 2878

To calculate the percentage of the used CPU, you can use Alexsis Wilke advice for using getrusage() and divide by the total runtime as you calculated in your question. This will give you the average percentage of the used CPU for a single CPU. If you are using many processors, you can also divide by the number of processors.

Upvotes: 2

Related Questions