Velocity Reviews > Measuring time differences

# Measuring time differences

Dominik Wallner
Guest
Posts: n/a

 12-16-2005
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

coder1024
Guest
Posts: n/a

 12-16-2005
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.

Keith Thompson
Guest
Posts: n/a

 12-16-2005
"coder1024" <(E-Mail Removed)> 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_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.

Keith Thompson
Guest
Posts: n/a

 12-16-2005
Dominik Wallner <(E-Mail Removed)> 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_Keith) (E-Mail Removed) <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.

Michael Mair
Guest
Posts: n/a

 12-16-2005
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.programmer

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.

Martin Ambuhl
Guest
Posts: n/a

 12-16-2005
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.com 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

Flash Gordon
Guest
Posts: n/a

 12-16-2005
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.

Flash Gordon
Guest
Posts: n/a

 12-16-2005
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.

Chuck F.
Guest
Posts: n/a

 12-16-2005
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)

pete
Guest
Posts: n/a

 12-16-2005
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