Elapsed time in milliseconds in C, please help! 
Author Message
 Elapsed time in milliseconds in C, please help!

Hi! I have to calculate how long (with millisecond precision) a piece of
my C code takes to execute. I don't know how I can do it. I tried many
functions: time(), ecc... but they have precision of seconds or I can't
figure out how to use them. I am a beginner. I am using gcc under Linux.
Can anyone help me with an enlightening piece of source code ? Thank you
in advance!


Mon, 09 Dec 2002 03:00:00 GMT  
 Elapsed time in milliseconds in C, please help!

Quote:

> Hi! I have to calculate how long (with millisecond precision) a piece of
> my C code takes to execute. I don't know how I can do it. I tried many
> functions: time(), ecc... but they have precision of seconds or I can't
> figure out how to use them.

There is no function to do this using ISO C; as you correctly note, the
<time.h> functions barely guarantee even 1 second precision.
There is possibly a way around this, though: repeat your code a thousand
times. Measure how long this takes. Divide by a thousand.

Richard



Mon, 09 Dec 2002 03:00:00 GMT  
 Elapsed time in milliseconds in C, please help!
Depends on what kind of time you are attempting to measure.  clock()
meansures process time.  That is, the total amount of time the CPU has
spent processing on the program.  In a task switching environment,
this won't coincide with real time.

If you're planning on benchmarking some code, clock() may be an
option.  Usually when people ask this sort of question they're looking
for real time though.

On Thu, 22 Jun 2000 11:13:08 -0400, Russell Harper

Quote:

>Try clock() and the conversion factor CLOCKS_PER_SEC - no guarantee
>that you'll get millisecond resolution.

>Russell


>> Hi! I have to calculate how long (with millisecond precision) a piece of
>> my C code takes to execute. I don't know how I can do it. I tried many
>> functions: time(), ecc... but they have precision of seconds or I can't
>> figure out how to use them. I am a beginner. I am using gcc under Linux.
>> Can anyone help me with an enlightening piece of source code ? Thank you
>> in advance!



Mon, 09 Dec 2002 03:00:00 GMT  
 Elapsed time in milliseconds in C, please help!

Quote:

> Hi! I have to calculate how long (with millisecond precision) a piece of
> my C code takes to execute. I don't know how I can do it. I tried many
> functions: time(), ecc... but they have precision of seconds or I can't
> figure out how to use them. I am a beginner. I am using gcc under Linux.
> Can anyone help me with an enlightening piece of source code ? Thank you
> in advance!

Check the man pages on the select() function.  I know it seems like an
unlikely method, but I have read that the select(), when used properly, will
yield a semi-portable way of producing sub-second time precision.

Good Luck,

Marshall.



Mon, 09 Dec 2002 03:00:00 GMT  
 Elapsed time in milliseconds in C, please help!
: Hi! I have to calculate how long (with millisecond precision) a piece of
: my C code takes to execute. I don't know how I can do it. I tried many
: functions: time(), ecc... but they have precision of seconds or I can't
: figure out how to use them. I am a beginner. I am using gcc under Linux.
: Can anyone help me with an enlightening piece of source code ? Thank you
: in advance!

In ANSI/ISO C (which is the topic of comp.lang.c) you can use the clock()
function and the CLOCKS_PER_SEC macro, but this is not guarranteed to
give you the precision you desire.  You may need to resort to platform
specific (under Linux, that would be POSIX, BSD, or SYSV) methods
for determining the elapsed CPU time in milliseconds.  Try reading
the man page for getrusage().  The Linux programming experts live in
comp.os.linux.development.apps, so you will have better luck posting a
Linux specific question there rather than in comp.lang.c.

Good Luck,

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html



Mon, 09 Dec 2002 03:00:00 GMT  
 Elapsed time in milliseconds in C, please help!
Hello people!!! In Linux we have a function call getdaytime or something
with get... with this function you may have milliseconds. See man pages (of
course if you have Linux, remember that this function isnt ANSI C)
Quote:

> Hi! I have to calculate how long (with millisecond precision) a piece of
> my C code takes to execute. I don't know how I can do it. I tried many
> functions: time(), ecc... but they have precision of seconds or I can't
> figure out how to use them. I am a beginner. I am using gcc under Linux.
> Can anyone help me with an enlightening piece of source code ? Thank you
> in advance!



Sun, 15 Dec 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. help getting time in milliseconds

2. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

3. Fine measurement of elapsed CPU time

4. how much time has elapsed

5. Elapsed Time

6. how do I get date from elapsed time

7. Measuring real time elapsed in a C program.

8. Elapsed time in C

9. Elapsed time in ms with ansi C

10. Elapsed time in subsecond

11. Elapsed time ?

12. Getting elapsed time of program ?

 

 
Powered by phpBB® Forum Software