Operating System - HP-UX
1752781 Members
6325 Online
108789 Solutions
New Discussion юеВ

Re: Measuring execution time of a C function, with high precision

 
SOLVED
Go to solution
Raghava_1
Occasional Advisor

Measuring execution time of a C function, with high precision

Hi,

We have a function (let's call it func_to_be_tested()) in our codebase. A drastic change has gone to this function during a recent fix. As a comparison, I need to measure the execution time for this particular function with my change, and compare that to the execution time of old/stock source.

something like

void main() {
/* get start time */
func_to_be_tested();
/* get end time */
/* time = end - start , print it */
}

The simplest way seemed to be using gettimeofday() but AFAIK this does not take into account the fact that it is not just my process running on the machine but a whole lot of others too. So, am trying to see if I can use any other API or clock/timer mechanism to measure the time spent by CPU (both the user CPU time + system CPU time) just for this particular function over a run, (and which does not take into account the time when my process gets off CPU and other process are running on the CPU)

Will HP Caliper help in this regard? OR can we use getrusage() and such APIs and obtain the required data?

Please suggest.

Basically, am of the opinion that I should be rather getting to know the number of CPU cycles spent on the particular function, to arrive at the required data. Please let me know if my understanding is incorrect.

Thanks.
4 REPLIES 4
Matti_Kurkela
Honored Contributor
Solution

Re: Measuring execution time of a C function, with high precision

man 2 times

http://docs.hp.com/en/B2355-60127/times.2.html

Run times() once before calling your function, and store the results.
Call your function.
Run times() again. Subtract to get the user & system times spent by your function.

For extra precision, replace "call your function" with "call your function 1000 times with a simple loop", if your function allows that. Then divide your time result with 1000.

MK
MK
Dennis Handly
Acclaimed Contributor

Re: Measuring execution time of a C function, with high precision

>void main() {

(This is illegal, main must be int.)

>Will HP Caliper help in this regard?

Yes provided the function takes enough time.

>Or can we use getrusage()

This probably isn't granular enough, unless you call it many times as MK suggested.

>I should be rather getting to know the number of CPU cycles spent on the particular function,

Provided you aren't making any system calls, or making the exact same ones.
Laurent Menase
Honored Contributor

Re: Measuring execution time of a C function, with high precision

you can use pstat_getproc()

pstat_getptoc(buf,sizeof(struct pst_status),1,getpid());
and use pst_usercycles, pst_systemcycles counters.
rick jones
Honored Contributor

Re: Measuring execution time of a C function, with high precision

As already mentioned, Caliper could indeed be helpful assuming your routine runs long enough - either by dint of being a long routine, or by being called a lot. You would want to normalize the time spent in the routine to the quantity of work done by the program.

If you can ensure that your program runs on a CPU "by itself" such that it is unlikely to be interrupted by another program etc, then you could also take the gettimeofday() path - or for less timing overhead, use gethrtime(). If you really wanted to get bare-bones, you would read CR16 on PA-RISC (the interval counter - perhaps it is CR15, the memory fades...) or the ITC (name?) on Itanium.
there is no rest for the wicked yet the virtuous have no pillows