Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

effectively measuring the speed of a function

Status
Not open for further replies.

keak

Programmer
Sep 12, 2005
247
CA
Hi there,
I am trying to write some tests to see how long my function takes to execute different inputs. I have :

Code:
    clock_t start, finish;
    start = clock();
    function1 (input1, input2);
    finish = clock();
printf ("start: %d end: %d\n", start, finish);

But even if I give large input, the start/finish time always seem to be the same. I was wondering if there was a more accurate/effecive way in testing a function's execution time.
 
The simple answer is to execute the function many times (say 1000) to get a better idea of the real time it takes. The problem with timing a single iteration is that all the other overheads dominate and give you a very false reading.


Trojan.
 
I did a 1000 rounds and the start time is start and end times are still the same for all entries.

I was thinking more on the line of getting more finely grained time measurements; I find it strange that I don't see any timegap, even when I pass over very large values to the function. Is there any logic error that I maybe missing here?
 
When you said you do 1000 iterations, do you mean you do it like this:
Code:
clock_t start, finish;
start = clock();
for ( int i = 0; i < 1000; ++i )
{
    function1 (input1, input2);
}
finish = clock();
printf ("start: %d end: %d\n", start, finish);
If you don't see a difference with that, try using 100,000 or more iterations.
 
Also, to see the number of milliseconds, do :

int iTimeMillis = (int)(( finish - start) *1E3/CLOCKS_PER_SEC);

which will give you a little more readable idea of timings

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
> I am trying to write some tests to see how long my function takes to execute different inputs
Which OS/Compiler?

Some systems have much faster clocks that clock(), which is really like trying to use a sun-dial to measure a 100m race.

> I did a 1000 rounds and the start time is start and end times are still the same for all entries
So keep multiplying the iterations by 10 until you get a meaningful run of between 1 and 10 seconds.


--
 
Another way is to run it for 10 seconds and see how many times it runs. Dividing my 10 then gets you how many it runs per second.

Just set a timer to send a signal in 10 seconds, then put your function in an infinite loop. The signal breaks the loop and reports the results. Here's a quick and dirty...
Code:
#include    <sys/time.h>
#include    <signal.h>
#include    <stdio.h>
#include    <stdlib.h>

static int cnt;
#define ITIME   10      /* Number of seconds to run */

void end_report ()
{
    printf ("Did %f per second.\n", ((float) cnt) / ((float) ITIME));
    exit(0);
}

main ()
{
    struct itimerval itv;

    bzero(&itv, sizeof (itv));

    printf ("Running for %d seconds ...\n", ITIME);

    signal (SIGVTALRM, end_report);
    itv.it_value.tv_sec = ITIME;
    itv.it_value.tv_usec = 0;
    setitimer (ITIMER_VIRTUAL, &itv, NULL);

    for (cnt = 0;; cnt++)
        {
        your_function_goes_here("parameters here");
        }
}
Or something like that.
 
hi
As per my understanding, the clock() will not give the time for the function execution. Since it will depend upon the OS on which the program is running. Also the pre-empting nature of the operating systems [in many cases] may not give the exact time duration of the function execution.

thanks
sanjay
 
To the people saying clock() is not accurate ...

... whats your suggestion for an alternative then ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Have you tried my suggestion? I use it for timing function calls all the time. It reports how many calls per second. For example, if it reports that it ran 547 per second, that means your function takes 1.83 milliseconds. If it reports 12876 persecond, that's 77.66 microseconds per function call

Milliseconds = ( 1 / #-per-second ) * 1000
Microseconds = ( 1 / #-per-second ) * 1000000

You have to do a little math, but that's a much finer resolution that any system clock I've seen. And it's accurate too.
 
Sam, I was directing my comment to those who stated that clock() was inaccurate (not saying ti isn't - I'm just genuinely interested). Say I had a function that lasted around 5 seconds to execute, but wanted to know that function's single execute time in milliseconds - not the average over a signal's alarm period as in your example ....

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top