1

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?

5
  • 2
    Check getrusage(), it's much better to know how much your specific process took instead of total real time. Commented May 3, 2017 at 16:07
  • You will need to specify what is the target OS, what kind of CPU usage do you want to know? Cycles count? Time spent as runnable? Clock time? Running time? Usage in percentage? Commented May 3, 2017 at 16:07
  • Not sure it's what you want, but if you're looking for a micro-benchmark library, try out this one by Google: github.com/google/benchmark. It measures CPU-time per iteration, as well as other things.
    – ehudt
    Commented May 3, 2017 at 16:08
  • @LiranFunaro Sorry, I want usage in percentage and if it is possible, running time. Can you help me?
    – PRVS
    Commented May 3, 2017 at 16:20
  • 1
    @AlexisWilke thank you so much! For memory It's good!
    – PRVS
    Commented May 3, 2017 at 16:20

1 Answer 1

2

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.

1
  • Thanks for the answer!
    – PRVS
    Commented May 5, 2017 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.