user304584
user304584

Reputation: 507

How do I measure the time my function, and only my function is using the CPU

I am using GCC on ubuntu, and I want a function that does measures the time that my function uses the CPU. I do not want to include the time that the CPU spent on background processes.

For example, if my function used the CPU for 10 ms, then the processor processed some other data for 20 ms, then continued working on my function for 8 ms. I want it to return 18ms, not 38. I don't mind if the result was in CPU ticks.

Upvotes: 2

Views: 328

Answers (3)

Richard Vock
Richard Vock

Reputation: 1468

In addition to the answer of Baris Demiray I can personally recommend the callgrind tool (as well as any other tool) of the valgrind toolset. This is especially useful in combination with kcachegrind/qcachegrind which should be available through the ubuntu repositories.

A very brief description of both tools, gprof and callgrind can be found here: http://kcachegrind.sourceforge.net/html/Introduction.html

Upvotes: 1

Baris Demiray
Baris Demiray

Reputation: 1607

What you want to do is called profiling and gprof on Linux is quite handy for that, see http://www.ibm.com/developerworks/library/l-gnuprof.html.

Upvotes: 2

Werner Henze
Werner Henze

Reputation: 16726

You want to take a look at clock() (man 3 clock).

Upvotes: -2

Related Questions