By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 2,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

getting elapsed time with accuracy in milliseconds

P: n/a
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.

Expand|Select|Wrap|Line Numbers
  1. ...
  2. start_time = time(NULL);
  3. ...
  4. ...
  5. ...
  6. printf(" - time elapsed: %li.\n", time(NULL) - start_time);
  7. // the above statement always returns the value 0, why??
  8. ...
  9.  

--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
“I'm smart enough to know that I'm dumb.”
- Richard P Feynman
Mar 9 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Fri, 09 Mar 2007 03:09:30 -0600, Spitfire <ti**********@gmail.com>
wrote:
I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.

Expand|Select|Wrap|Line Numbers
  1.   ...
  2.   start_time = time(NULL);
  3.   ...
  4.   ...
  5.   ...
  6.   printf(" - time elapsed: %li.\n", time(NULL) - start_time);
  7.   // the above statement always returns the value 0, why??
  8.   ...
  9.  
Because time() will only return seconds.

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

---
Stefan Pendl
Mar 9 '07 #2

P: n/a
Stefan Pendl wrote:
Because time() will only return seconds.

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

---
Stefan Pendl
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!!!
--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
“I'm smart enough to know that I'm dumb.”
- Richard P Feynman
Mar 9 '07 #3

P: n/a
On Mar 9, 1:09 am, Spitfire <timid.gen...@gmail.comwrote:
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.

Expand|Select|Wrap|Line Numbers
  1.    ...
  2.    start_time = time(NULL);
  3.    ...
  4.    ...
  5.    ...
  6.    printf(" - time elapsed: %li.\n", time(NULL) - start_time);
  7.    // the above statement always returns the value 0, why??
  8.    ...
  9.  

--
_ _ _]{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.

Mar 9 '07 #4

P: n/a
On Fri, 09 Mar 2007 03:09:30 -0600, Spitfire wrote:
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.

Expand|Select|Wrap|Line Numbers
  1.    ...
  2.    start_time = time(NULL);
  3.    ...
  4.    ...
  5.    ...
  6.    printf(" - time elapsed: %li.\n", time(NULL) - start_time);
  7.    // the above statement always returns the value 0, why??
  8.    ...
  9.  
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
Mar 9 '07 #5

P: n/a
Stefan Pendl said:

<snip>
>
[...] time() will only return seconds.
The Standard doesn't specify or require this.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #6

P: n/a
Stefan Pendl <st**********@nospam.comwrites:
On Fri, 09 Mar 2007 03:09:30 -0600, Spitfire <ti**********@gmail.com>
wrote:
> I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.

Expand|Select|Wrap|Line Numbers
  1.   ...
  2.   start_time = time(NULL);
  3.   ...
  4.   ...
  5.   ...
  6.   printf(" - time elapsed: %li.\n", time(NULL) - start_time);
  7.   // the above statement always returns the value 0, why??
  8.   ...
  9.   

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 9 '07 #7

P: n/a
On Mar 9, 10:12 am, "user923005" <dcor...@connx.comwrote:
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++) ;" ...

Mar 10 '07 #8

P: n/a
user923005 wrote:
On Mar 9, 1:09 am, Spitfire <timid.gen...@gmail.comwrote:
> 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.

Expand|Select|Wrap|Line Numbers
  1.    ...
  2.    start_time = time(NULL);
  3.    ...
  4.    ...
  5.    ...
  6.    printf(" - time elapsed: %li.\n", time(NULL) - start_time);
  7.    // the above statement always returns the value 0, why??
  8.    ...
  9.    

--
_ _ _]{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 :)

--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
“I'm smart enough to know that I'm dumb.”
- Richard P Feynman
Mar 12 '07 #9

P: n/a
Duncan Muirhead wrote:
On Fri, 09 Mar 2007 03:09:30 -0600, Spitfire wrote:
> 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.

Expand|Select|Wrap|Line Numbers
  1.    ...
  2.    start_time = time(NULL);
  3.    ...
  4.    ...
  5.    ...
  6.    printf(" - time elapsed: %li.\n", time(NULL) - start_time);
  7.    // the above statement always returns the value 0, why??
  8.    ...
  9.    
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!

--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
“I'm smart enough to know that I'm dumb.”
- Richard P Feynman
Mar 12 '07 #10

P: n/a
Keith Thompson wrote:
Stefan Pendl <st**********@nospam.comwrites:
>On Fri, 09 Mar 2007 03:09:30 -0600, Spitfire <ti**********@gmail.com>
wrote:
>> I've a requirement to find the elapsed time between two function
calls. I need to find the time elapsed accurate to 1 millisecond.

Expand|Select|Wrap|Line Numbers
  1.   ...
  2.   start_time = time(NULL);
  3.   ...
  4.   ...
  5.   ...
  6.   printf(" - time elapsed: %li.\n", time(NULL) - start_time);
  7.   // the above statement always returns the value 0, why??
  8.   ...
  9.   
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.

--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
“I'm smart enough to know that I'm dumb.”
- Richard P Feynman
Mar 12 '07 #11

P: n/a
Spitfire <ti**********@gmail.comwrites:
Keith Thompson wrote:
>Stefan Pendl <st**********@nospam.comwrites:
[...]
>>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.
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 12 '07 #12

P: n/a
Spitfire wrote:
>
.... 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 13 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.