473,943 Members | 1,590 Online

# Measuring time differences

Hi!

I'm currently implementing a program which measures voltages through an
external USB-AD-converter. It should output those values as time/voltage
pairs.

My problem is to measure the time to output (time elapsed since program
start would do just fine) - it should be as precise as possible, as
there may be only differences in milliseconds between two measurements.

Currently I'm using (clock()/float(CLK_TCK)) as time value, which is not
accurat enough as I get something like:

Time Voltage
0 0
0 0.062474
0 0.124792
0.01 0.186798
0.01 0.248337
0.01 0.309255
0.01 0.3694
0.02 0.428622
0.02 0.486773
0.02 0.543707
0.02 0.599282
0.02 0.653359
0.03 0.705803
0.03 0.756483
0.03 0.805272
0.03 0.852048
0.03 0.896695
0.04 0.939101
0.04 0.979159
0.04 1.01677

So I need a more precise way of measuring time differences. What
functions are available in C to do so? I'd like to keep my program as
portable as possible, but finally it should run only on a Windows/DOS
machine, so I can also use DOS-only functions if neccessary.

Thank you very much!
Yours,
Dominik Wallner
Dec 16 '05 #1
74 7866
you could use gettimeofday() when you start and then call it
periodically after that, comparing the values with the one at the start
of the experiment. gettimeofday() returns the current time in seconds
and microseconds.

Dec 16 '05 #2
"coder1024" <co*******@gmai l.com> writes:
you could use gettimeofday() when you start and then call it
periodically after that, comparing the values with the one at the start
of the experiment. gettimeofday() returns the current time in seconds
and microseconds.

gettimeofday() is not standard C. In fact, it's defined by the POSIX
standard, and since the OP is using a Windows/DOS environment, it's
likely not to be available.

--
Keith Thompson (The_Other_Keit h) 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.
Dec 16 '05 #3
Dominik Wallner <do*******@yaho o.de> writes:
I'm currently implementing a program which measures voltages through an
external USB-AD-converter. It should output those values as time/voltage
pairs.

My problem is to measure the time to output (time elapsed since program
start would do just fine) - it should be as precise as possible, as
there may be only differences in milliseconds between two measurements.

Currently I'm using (clock()/float(CLK_TCK)) as time value, which is not
accurat enough as I get something like: [snip]

clock() measure CPU time, not real time.
So I need a more precise way of measuring time differences. What
functions are available in C to do so? I'd like to keep my program as
portable as possible, but finally it should run only on a Windows/DOS
machine, so I can also use DOS-only functions if neccessary.

There are no portable functions in standard C to measure time with any
particular precision. The time() function returns a value of time
time_t, which is merely an arithmetic type capable of representing
times; a typical resolution is 1 second, but there are no guarantees.

You should ask in a DOS or Windows newsgroup.

--
Keith Thompson (The_Other_Keit h) 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.
Dec 16 '05 #4
Dominik Wallner wrote:
Hi!

I'm currently implementing a program which measures voltages through an
external USB-AD-converter. It should output those values as time/voltage
pairs.

My problem is to measure the time to output (time elapsed since program
start would do just fine) - it should be as precise as possible, as
there may be only differences in milliseconds between two measurements.

Currently I'm using (clock()/float(CLK_TCK)) as time value, which is not
accurat enough as I get something like:

Time Voltage
0 0
0 0.062474
0 0.124792
0.01 0.186798
0.01 0.248337
0.01 0.309255
0.01 0.3694
0.02 0.428622
0.02 0.486773
0.02 0.543707
0.02 0.599282
0.02 0.653359
0.03 0.705803
0.03 0.756483
0.03 0.805272
0.03 0.852048
0.03 0.896695
0.04 0.939101
0.04 0.979159
0.04 1.01677

So I need a more precise way of measuring time differences. What
functions are available in C to do so? I'd like to keep my program as
portable as possible, but finally it should run only on a Windows/DOS
machine, so I can also use DOS-only functions if neccessary.

Wrap your time measurement into functions or macros, e.g.
START_TIME
CURR_TIME
STOP_TIME
and put your non-portable solution behind that. This way,
you only have to change the function or macro definition.

may be
comp.os.ms-windows.program mer

Note: Above, you are using float; consider using double if
you want to be sure to have more than six significant digits.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dec 16 '05 #5
coder1024 wrote:
you could use gettimeofday() when you start and then call it
periodically after that, comparing the values with the one at the start
of the experiment. gettimeofday() returns the current time in seconds
and microseconds.

There are two horrible errors in your post:
1) You provide no context for your "answer." I attribute this to your
not knowing how to use groups.google.c om correctly.
1a) Had you followed normal civil usenet practice and followed the
newsgroup before posting, you would know how to do this
correctly.
2) You provide an "answer" that uses a function that is not part of the
standard C programming language. gettimeofday() has no definition in C.
What arguments it takes, what values it returns, and what its side
effects might be are completely undefined in C.
2a) Had you followed normal civil usenet practice and followed the
newsgroup before posting, you would know not to provide such
Dec 16 '05 #6
coder1024 wrote:
you could use gettimeofday() when you start and then call it
periodically after that, comparing the values with the one at the start
of the experiment. gettimeofday() returns the current time in seconds
and microseconds.

Please provide context, people may not have seen the message you are
replying to since Google is only one of thousands of servers and
propogation between servers is not perfect. See

As to gettimeofday, that is a Unix function and is NOT part of the C
language and standard library. So, apart from being off topic here (we
only deal with standard C) it won't solve the OPs problem if s/he is
using DOS or Windows (which is the OPs situation).
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 16 '05 #7
Dominik Wallner wrote:

<snip>
My problem is to measure the time to output (time elapsed since program
start would do just fine) - it should be as precise as possible, as
there may be only differences in milliseconds between two measurements.

Currently I'm using (clock()/float(CLK_TCK)) as time value,
clock give you CPA time which, on a multi-tasking OS (and even in DOS if
TSRs are eating processing time) is *not* the same as real time, and I
strongly suspect you want real time rather than CPU time.
which is not
accurat enough as I get something like:

Time Voltage
0 0
0 0.062474
0 0.124792
<snip>
So I need a more precise way of measuring time differences. What
functions are available in C to do so? I'd like to keep my program as
portable as possible, but finally it should run only on a Windows/DOS
machine, so I can also use DOS-only functions if neccessary.

There is no standard method to solve your problem. Standard C provides a
time function which returns a measure of calendar time, but there is
nothing requiring it to be accurate enough for your purposes (and it
probably is not accurate enough) so you will need to leave the realms of
standard C and do something specific to you implementation. For advice
on implementation specific functions you will have to ask in groups
dedicated to the systems you are interested in, such as one of the
microsoft news groups. Although be aware that the best answers for DOS
and Windows are probably significantly different, so you may well have
to decide which you want to target.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 16 '05 #8
Michael Mair wrote:

Dominik Wallner wrote:

Currently I'm using (clock()/float(CLK_TCK)) as time value, which is

Note: Above, you are using float; consider using double if
you want to be sure to have more than six significant digits.

For floating types, consider using double,
unless you have a special reason not to.

--
pete
Dec 16 '05 #9
Dominik Wallner wrote:

I'm currently implementing a program which measures voltages
through an external USB-AD-converter. It should output those
values as time/voltage pairs.

My problem is to measure the time to output (time elapsed since
program start would do just fine) - it should be as precise as
possible, as there may be only differences in milliseconds
between two measurements.

Currently I'm using (clock()/float(CLK_TCK)) as time value,
which is not accurat enough as I get something like:
.... snip ...
So I need a more precise way of measuring time differences. What
functions are available in C to do so? I'd like to keep my
program as portable as possible, but finally it should run only
on a Windows/DOS machine, so I can also use DOS-only functions
if neccessary.

You are out of luck on this newsgroup, which deals only with the
portable standard C languages. That provides no guarantees about
clock resolution etc. For non-portable solutions for your
particular systems find a group that deals with your system. Such
things are off-topic here.

under DOS/Windows.

--
<http://www.ungerhu.com/jxh/clc.welcome.txt >
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware. com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
Dec 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.