Timing intervals <1sec

How do I time an interval between two lines of code with millisecond
accuracy? In the docs I have found:

start_time = time(NULL);
processing();
finish_time = time(NULL);
printf(“Processing took %f seconds\n”, difftime(finish_time, start_time));

but this rounds to the nearest second. Is there a similar method to display
the difference in milliseconds between the start and finish times?

Paul.

Paul Jones <paul.jones@bnc.ox.ac.uk> wrote:

How do I time an interval between two lines of code with millisecond
accuracy? In the docs I have found:
start_time = time(NULL);
processing();
finish_time = time(NULL);
printf(“Processing took %f seconds\n”, difftime(finish_time, start_time));
but this rounds to the nearest second. Is there a similar method to
display the difference in milliseconds between the start and finish times?

struct timespec t0, t1;
clock_gettime(CLOCK_REALTIME, &t0);
processing()
clock_gettime(CLOCK_REALTIME, &t1);
printf(“Processing took %ldms\n”,
(t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_nsec - t0.tv_nsec) / 1000000);

“John Garvey” <jgarvey@qnx.com> wrote in message
news:a27a81$s5$2@nntp.qnx.com

Paul Jones <> paul.jones@bnc.ox.ac.uk> > wrote:
How do I time an interval between two lines of code with millisecond
accuracy? In the docs I have found:
start_time = time(NULL);
processing();
finish_time = time(NULL);
printf(“Processing took %f seconds\n”, difftime(finish_time,
start_time));
but this rounds to the nearest second. Is there a similar method to
display the difference in milliseconds between the start and finish
times?

struct timespec t0, t1;
clock_gettime(CLOCK_REALTIME, &t0);
processing()
clock_gettime(CLOCK_REALTIME, &t1);
printf(“Processing took %ldms\n”,
(t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_nsec - t0.tv_nsec) / 1000000);

Check out
http://qdn.qnx.com/support/docs/neutrino_2.11_en/lib_ref/c/clockcycles.html
has some drawback but will get you a lot more precision.

  • Mario

John Garvey wrote:

Paul Jones <> paul.jones@bnc.ox.ac.uk> > wrote:
How do I time an interval between two lines of code with millisecond
accuracy? In the docs I have found:
start_time = time(NULL);
processing();
finish_time = time(NULL);
printf(“Processing took %f seconds\n”, difftime(finish_time, start_time));
but this rounds to the nearest second. Is there a similar method to
display the difference in milliseconds between the start and finish times?

struct timespec t0, t1;
clock_gettime(CLOCK_REALTIME, &t0);
processing()
clock_gettime(CLOCK_REALTIME, &t1);
printf(“Processing took %ldms\n”,
(t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_nsec - t0.tv_nsec) / 1000000);

for sub milli second time resolution … make a search with’timer’ at

http://quics.qnx.com/cgi-bin/dir_find.cgi?/usr/free/

Regards

Armin