Timing Procedures

Hello,

I am trying to setup a method for timing procedures that I have written,
to establish the rate at which my process can run. Right now I am using
CLOCK_REALTIME to get a start and stop point, such as

clock_gettime( CLOCK_REALTIME, &start );
MyCustomFunction();
clock_gettime( CLOCK_REALTIME, &stop );

Is this a reliable method for timing functions? I run into problems
sometimes when I place a call to sprintf() between the clock_gettime()
calls, such as

clock_gettime( CLOCK_REALTIME, &start );
sprintf( str, “file.txt” );
MyCustomFunction( str );
clock_gettime( CLOCK_REALTIME, &stop );

The times are way off, like in the order to 10E9. Does Anyone have any
suggestions for ways of getting around this problem, or better ways of
testing the timing of functions?

Thanks,
Mathew.

Mathew Asselin <m2asselin@yahoo.com> wrote:

Hello,

I am trying to setup a method for timing procedures that I have written,
to establish the rate at which my process can run. Right now I am using
CLOCK_REALTIME to get a start and stop point, such as

clock_gettime( CLOCK_REALTIME, &start );
MyCustomFunction();
clock_gettime( CLOCK_REALTIME, &stop );

Is this a reliable method for timing functions? I run into problems
sometimes when I place a call to sprintf() between the clock_gettime()
calls, such as

I would suggest using ClockCycles() rather than clock_gettime –
it will give you quite a bit more precise timing values. (On
most CPUs at least.) The resolution of clock_gettime() is
(usually, unless reconfigured) 1 ms – often not enough.


clock_gettime( CLOCK_REALTIME, &start );
sprintf( str, “file.txt” );
MyCustomFunction( str );
clock_gettime( CLOCK_REALTIME, &stop );

The times are way off, like in the order to 10E9. Does Anyone have any
suggestions for ways of getting around this problem, or better ways of
testing the timing of functions?

When doing the stop-start, are you only doing the math with the
nanoseconds field, or do you check for wrap of nanoseconds, by
also getting the seconds field?

Correct calculation of the difference would be:

#define BILLION 100010001000
delta = ((uint64_t) stop.tv_sec * BILLION + (uint64_t) stop.tv_nsec) -
((uint64_t) start.tv_sec * BILLION + (uint64_t) start.tv_nsec);
printf(“time taken was: %lld nanoseconds\n”, delta );

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

David Gibbs <dagibbs@qnx.com> wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Hello,

I am trying to setup a method for timing procedures that I have written,
to establish the rate at which my process can run. Right now I am using
CLOCK_REALTIME to get a start and stop point, such as

clock_gettime( CLOCK_REALTIME, &start );
MyCustomFunction();
clock_gettime( CLOCK_REALTIME, &stop );

Is this a reliable method for timing functions? I run into problems
sometimes when I place a call to sprintf() between the clock_gettime()
calls, such as

I would suggest using ClockCycles() rather than clock_gettime –
it will give you quite a bit more precise timing values. (On
most CPUs at least.) The resolution of clock_gettime() is
(usually, unless reconfigured) 1 ms – often not enough.

Also, don’t forget that ClockCycles gives you the snapshot of the time
on the CPU chip so beware of using it in an SMP environment :slight_smile:

Cheers,
-RK

clock_gettime( CLOCK_REALTIME, &start );
sprintf( str, “file.txt” );
MyCustomFunction( str );
clock_gettime( CLOCK_REALTIME, &stop );

The times are way off, like in the order to 10E9. Does Anyone have any
suggestions for ways of getting around this problem, or better ways of
testing the timing of functions?

When doing the stop-start, are you only doing the math with the
nanoseconds field, or do you check for wrap of nanoseconds, by
also getting the seconds field?

Correct calculation of the difference would be:

#define BILLION 100010001000
delta = ((uint64_t) stop.tv_sec * BILLION + (uint64_t) stop.tv_nsec) -
((uint64_t) start.tv_sec * BILLION + (uint64_t) start.tv_nsec);
printf(“time taken was: %lld nanoseconds\n”, delta );

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com


[If replying via email, you’ll need to click on the URL that’s emailed to you
afterwards to forward the email to me – spam filters and all that]
Robert Krten, PDP minicomputer collector http://www.parse.com/~pdp8/

Suggest parentheses around 100010001000 -

dB


David Gibbs wrote ~ 30 Jul 2004 14:00:23 GMT:

#define BILLION 100010001000

David Bacon <dbacon@qnx.com> wrote:

Suggest parentheses around 100010001000 -

Yeah, probably. Though, I think the C preprocessor will evaluate that
and just give 1000000000.

-David


dB


David Gibbs wrote ~ 30 Jul 2004 14:00:23 GMT:

#define BILLION 100010001000


Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

Depends on context. The following program will print both a
greeting and 1234567000000, because the C preprocessor
evaluates BILLION in order to do the static comparison with 55,
but it cannot legally do that when macro-expanding code. (If
parenthesized, the billion would get evaluated statically by
the compiler’s constant folding, a much later stage than
preprocessing of course, and the program would print 1.)

#include <stdio.h>
#define BILLION 100010001000
main() {
long long x = 1234567890;
long long y = x / BILLION;
#if BILLION > 55
printf (“hello dagibbs\n”);
#endif
printf ("%lld\n", x);
printf ("%lld\n", y);
}

dB


David Gibbs wrote ~ 30 Jul 2004 19:00:37 GMT:

David Bacon <> dbacon@qnx.com> > wrote:
Suggest parentheses around 100010001000 -

Yeah, probably. Though, I think the C preprocessor will evaluate that
and just give 1000000000.

-David


dB


David Gibbs wrote ~ 30 Jul 2004 14:00:23 GMT:

#define BILLION 100010001000


Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com