简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM