getting elapsed time with accuracy in milliseconds

S

Spitfire

I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.
The problem I'm facing right now is that, I'm using the 'time()'
function call for the purpose and regardless of how many time I invoke
the function within my program, I get the same output!!!
For better clarity consider the code snippet below.

Code:
   ...
   start_time = time(NULL);
   ...
   ...
   ...
   printf(" - time elapsed: %li.\n", time(NULL) - start_time);
   // the above statement always returns the value 0, why??
   ...
 
S

Stefan Pendl

I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.

Code:
...
start_time = time(NULL);
...
...
...
printf(" - time elapsed: %li.\n", time(NULL) - start_time);
// the above statement always returns the value 0, why??
...

Because time() will only return seconds.

You will have to use clock(), which uses clock ticks, which you can
convert to milliseconds.
 
S

Spitfire

Stefan said:
Because time() will only return seconds.

You will have to use clock(), which uses clock ticks, which you can
convert to milliseconds.
Thanks for the heads up on time()

And, regarding clock(), also gets me the same value like that of time()
every time I run it!!!
 
D

Duncan Muirhead

I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.
The problem I'm facing right now is that, I'm using the 'time()'
function call for the purpose and regardless of how many time I invoke
the function within my program, I get the same output!!!
For better clarity consider the code snippet below.

Code:
...
start_time = time(NULL);
...
...
...
printf(" - time elapsed: %li.\n", time(NULL) - start_time);
// the above statement always returns the value 0, why??
...
This is an operating system question, rather than a pure C one. You are
likely to get more informative answers from eg comp.unix.programming if
you are working on a unix like system. Assuming that, the answer is that
time returns an integer number of seconds, so that if the code timed takes
much less than a second you'll usually get zero. You could use
gettimeofday() to get time (in a struct timeval) down to a microsecond.
(Do you really want elapsed time. or do you want to measure how much cpu
the code is using? In the latter case you'd be better using getrusage())
Duncan
 
U

user923005

I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.
The problem I'm facing right now is that, I'm using the 'time()'
function call for the purpose and regardless of how many time I invoke
the function within my program, I get the same output!!!
For better clarity consider the code snippet below.

Code:
...
start_time = time(NULL);
...
...
...
printf(" - time elapsed: %li.\n", time(NULL) - start_time);
// the above statement always returns the value 0, why??
...

--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
"I'm smart enough to know that I'm dumb."
- Richard P Feynman
From the C-FAQ:
19.37: How can I implement a delay, or time a user's response, with
sub-
second resolution?

A: Unfortunately, there is no portable way. V7 Unix, and derived
systems, provided a fairly useful ftime() function with
resolution up to a millisecond, but it has disappeared from
System V and POSIX. Other routines you might look for on your
system include clock(), delay(), gettimeofday(), msleep(),
nap(), napms(), nanosleep(), setitimer(), sleep(), times(), and
usleep(). (A function called wait(), however, is at least under
Unix *not* what you want.) The select() and poll() calls (if
available) can be pressed into service to implement simple
delays. On MS-DOS machines, it is possible to reprogram the
system timer and timer interrupts.

Of these, only clock() is part of the ANSI Standard. The
difference between two calls to clock() gives elapsed execution
time, and may even have subsecond resolution, if CLOCKS_PER_SEC
is greater than 1. However, clock() gives elapsed processor time
used by the current program, which on a multitasking system may
differ considerably from real time.

If you're trying to implement a delay and all you have available
is a time-reporting function, you can implement a CPU-intensive
busy-wait, but this is only an option on a single-user, single-
tasking machine as it is terribly antisocial to any other
processes. Under a multitasking operating system, be sure to
use a call which puts your process to sleep for the duration,
such as sleep() or select(), or pause() in conjunction with
alarm() or setitimer().

For really brief delays, it's tempting to use a do-nothing loop
like

long int i;
for(i = 0; i < 1000000; i++)
;

but resist this temptation if at all possible! For one thing,
your carefully-calculated delay loops will stop working properly
next month when a faster processor comes out. Perhaps worse, a
clever compiler may notice that the loop does nothing and
optimize it away completely.

References: H&S Sec. 18.1 pp. 398-9; PCS Sec. 12 pp. 197-8,215-
6; POSIX Sec. 4.5.2.
 
K

Keith Thompson

Stefan Pendl said:
I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.

Code:
...
start_time = time(NULL);
...
...
...
printf(" - time elapsed: %li.\n", time(NULL) - start_time);
// the above statement always returns the value 0, why??
...

Because time() will only return seconds.

You will have to use clock(), which uses clock ticks, which you can
convert to milliseconds.

Um, no.

time() returns a result of type time_t, which is only guaranteed to be
an arithmetic type capable of representing times. A 1-second
resolution is common, but not required. clock() returns an indication
of CPU time, not elapsed time.

What you're trying to do is impossible in pure standard C, but is
likely to be very easy if you use some system-specific extension.
Consult your local documentation. If that fails, try a
system-specific newsgroup. See also question 19.37 in the comp.lang.c
FAQ, which user923005 already cited elsewhere in this thread.
 
C

christian.bau

For really brief delays, it's tempting to use a do-nothing loop
like

long int i;
for(i = 0; i < 1000000; i++)
;

but resist this temptation if at all possible! For one thing,
your carefully-calculated delay loops will stop working properly
next month when a faster processor comes out. Perhaps worse, a
clever compiler may notice that the loop does nothing and
optimize it away completely.

I used one compiler that was not clever enough to optimise it away,
but clever enough to unroll it eight times, effectively changing it to
"for(i = 0; i < 125000; i++) ;" ...
 
S

Spitfire

user923005 said:
I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.
The problem I'm facing right now is that, I'm using the 'time()'
function call for the purpose and regardless of how many time I invoke
the function within my program, I get the same output!!!
For better clarity consider the code snippet below.

Code:
...
start_time = time(NULL);
...
...
...
printf(" - time elapsed: %li.\n", time(NULL) - start_time);
// the above statement always returns the value 0, why??
...

--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
"I'm smart enough to know that I'm dumb."
- Richard P Feynman
From the C-FAQ:
19.37: How can I implement a delay, or time a user's response, with
sub-
second resolution?

A: Unfortunately, there is no portable way. V7 Unix, and derived
systems, provided a fairly useful ftime() function with
resolution up to a millisecond, but it has disappeared from
System V and POSIX. Other routines you might look for on your
system include clock(), delay(), gettimeofday(), msleep(),
nap(), napms(), nanosleep(), setitimer(), sleep(), times(), and
usleep(). (A function called wait(), however, is at least under
Unix *not* what you want.) The select() and poll() calls (if
available) can be pressed into service to implement simple
delays. On MS-DOS machines, it is possible to reprogram the
system timer and timer interrupts.

Of these, only clock() is part of the ANSI Standard. The
difference between two calls to clock() gives elapsed execution
time, and may even have subsecond resolution, if CLOCKS_PER_SEC
is greater than 1. However, clock() gives elapsed processor time
used by the current program, which on a multitasking system may
differ considerably from real time.

If you're trying to implement a delay and all you have available
is a time-reporting function, you can implement a CPU-intensive
busy-wait, but this is only an option on a single-user, single-
tasking machine as it is terribly antisocial to any other
processes. Under a multitasking operating system, be sure to
use a call which puts your process to sleep for the duration,
such as sleep() or select(), or pause() in conjunction with
alarm() or setitimer().

For really brief delays, it's tempting to use a do-nothing loop
like

long int i;
for(i = 0; i < 1000000; i++)
;

but resist this temptation if at all possible! For one thing,
your carefully-calculated delay loops will stop working properly
next month when a faster processor comes out. Perhaps worse, a
clever compiler may notice that the loop does nothing and
optimize it away completely.

References: H&S Sec. 18.1 pp. 398-9; PCS Sec. 12 pp. 197-8,215-
6; POSIX Sec. 4.5.2.
Sorry for the delayed acknowledgment. However, thought it would be
better to say what I finally used to get the work done.

gettimeofday() solved the problem :)
 
S

Spitfire

Duncan said:
I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.
The problem I'm facing right now is that, I'm using the 'time()'
function call for the purpose and regardless of how many time I invoke
the function within my program, I get the same output!!!
For better clarity consider the code snippet below.

Code:
...
start_time = time(NULL);
...
...
...
printf(" - time elapsed: %li.\n", time(NULL) - start_time);
// the above statement always returns the value 0, why??
...
This is an operating system question, rather than a pure C one. You are
likely to get more informative answers from eg comp.unix.programming if
you are working on a unix like system. Assuming that, the answer is that
time returns an integer number of seconds, so that if the code timed takes
much less than a second you'll usually get zero. You could use
gettimeofday() to get time (in a struct timeval) down to a microsecond.
(Do you really want elapsed time. or do you want to measure how much cpu
the code is using? In the latter case you'd be better using getrusage())
Duncan
Thanks!
 
S

Spitfire

Keith said:
Stefan Pendl said:
I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.

Code:
...
start_time = time(NULL);
...
...
...
printf(" - time elapsed: %li.\n", time(NULL) - start_time);
// the above statement always returns the value 0, why??
...
Because time() will only return seconds.

You will have to use clock(), which uses clock ticks, which you can
convert to milliseconds.

Um, no.

time() returns a result of type time_t, which is only guaranteed to be
an arithmetic type capable of representing times. A 1-second
resolution is common, but not required. clock() returns an indication
of CPU time, not elapsed time.

What you're trying to do is impossible in pure standard C, but is
likely to be very easy if you use some system-specific extension.
Consult your local documentation. If that fails, try a
system-specific newsgroup. See also question 19.37 in the comp.lang.c
FAQ, which user923005 already cited elsewhere in this thread.
I do know enough to contend with you on what is possible or impossible
with pure C. However, the I wish to highlight that gettimeofday() solved
the problem and so far I see no glitch in the time calculations. I'm
able to get a resolution of upto 1 microsecond.
 
K

Keith Thompson

Spitfire said:
I do know enough to contend with you on what is possible or impossible
with pure C. However, the I wish to highlight that gettimeofday()
solved the problem and so far I see no glitch in the time
calculations. I'm able to get a resolution of upto 1 microsecond.

Was that supposed to be "I do not know enough ..."?

I'm glad gettimeofday() worked for you, but just to make sure everyone
is aware of it, that function is not part of standard C, and strictly
speaking it's off-topic here.
 
C

CBFalconer

Spitfire said:
.... snip ...

I do know enough to contend with you on what is possible or
impossible with pure C. However, the I wish to highlight that
gettimeofday() solved the problem and so far I see no glitch
in the time calculations. I'm able to get a resolution of upto
1 microsecond.

No it didn't, because there is no such function in standard C. If
it exists on your system it is system specific. On mine calling it
causes diarhea and general incontinence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top