Measuring time differences

D

Dominik Wallner

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
 
C

coder1024

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.
 
K

Keith Thompson

coder1024 said:
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 read <http://cfaj.freeshell.org/google/>.

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.
 
K

Keith Thompson

Dominik Wallner said:
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.
 
M

Michael Mair

Dominik said:
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.

A good starting point to ask about system specific solutions
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
 
M

Martin Ambuhl

coder1024 said:
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
non-answers.
 
F

Flash Gordon

coder1024 said:
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
http://cfaj.freeshell.org/google/ for more details.

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).
 
F

Flash Gordon

Dominik Wallner wrote:

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

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.
 
C

Chuck F.

Dominik said:
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.

I expect you will get the best results from a DJGPP installation
under DOS/Windows.

--
Some useful references about C:
<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)
 
P

pete

Michael said:
Dominik Wallner wrote:
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.
 
C

coder1024

Please read <http://cfaj.freeshell.org/google/>.

ok, sorry for the lack of quotes. I used the google groups reply and
was hoping that would take care of putting it on context.
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.

I'm not aware of a standard C function to provide what the OP was
looking for. gettimeofday() whether or not its standard, will provide
what the OP is looking for, provided their development environment
provides it. its at least a chance of solving the OP problem.
 
C

coder1024

There are two horrible errors in your post

horrible? :)
You provide no context for your "answer." I attribute this to your
not knowing how to use groups.google.com correctly

sorry. was using google groups to post and figured it would handle
associating the reply with the post and properly disseminating the
reply to maintain that association.
You provide an "answer" that uses a function that is not part of the
standard C programming language. gettimeofday() has no definition in C.

I'm not aware of a standard call to do what the OP was asking for. I
provided this as a suggestion for the OP to try in case they're lucky
enough to have the function. In contrast with your reply which
provides no help to the OP. Granted, I can't argue with your "horrible
issues" all that much, but at least it was trying to provide some help.
 
K

Keith Thompson

coder1024 said:
ok, sorry for the lack of quotes. I used the google groups reply and
was hoping that would take care of putting it on context.

And the lack of attribution, which Google does provide if you use it
properly.
I'm not aware of a standard C function to provide what the OP was
looking for.

There isn't one.
gettimeofday() whether or not its standard, will provide
what the OP is looking for, provided their development environment
provides it. its at least a chance of solving the OP problem.

The OP said he was using DOS/Windows, which most likely doesn't
provide gettimeofday(). A redirection to an appropriate newsgroup is
far more likely to be helpful.
 
C

coder1024

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

You might want to consider looking at the Apache Portable Runtime
project (http://apr.apache.org/). Since portability is a concern this
might provide a nice C library to handle your time needs and other
issues you may come across as you continue your development.

For the specific time issue you've posted about, see the apr_time_now()
function in their time functions module
(http://apr.apache.org/docs/apr/group__apr__time.html#ga6)

apr_time_t apr_time_now (void) ;

where apr_time_t is microseconds since epoch. this may be better than
gettimeofday() as I originally suggested, since APR runs on a very wide
variety of platforms, where it might be hit and miss wrt whether or not
your platform/environment provides gettimeofday().

Of course, this isn't part of the standard language, but the APR does
provide a very portable set of C functions and so might be of value to
you.
 
C

coder1024

Keith said:
And the lack of attribution, which Google does provide if you use it
properly.

well, google provides 2 ways of replying. as I've now discovered,
clicking the reply which opens the text field right on the page doesn't
provide the quoting or attribution. doing the full, separate page
reply does. so I guess I'm due for another 100 lashes. well, now I
know.

Keith said:
There isn't one.

which would suggest the OP will need to look to a non-standard
solution.

Keith said:
The OP said he was using DOS/Windows, which most likely doesn't
provide gettimeofday(). A redirection to an appropriate newsgroup is
far more likely to be helpful.

you're probably right. see my later reply. I think the OP could be
well served by checking out the APR.
 
R

Randy Howard

coder1024 wrote
(in article
well, google provides 2 ways of replying. as I've now discovered,
clicking the reply which opens the text field right on the page doesn't
provide the quoting or attribution. doing the full, separate page
reply does. so I guess I'm due for another 100 lashes. well, now I
know.

Learning from past mistakes is a really good sign. :)

which would suggest the OP will need to look to a non-standard
solution.

Precisely. Such a solution is off-topic in this newsgroup, but
decidedly on-topic in newsgroups populated with people familiar
with his platform. Here's the conundrum. If you answer an
off-topic question in a newsgroup, there is the possibility that
you are correct, in which case you do some good. There is also
the possibility that you are incorrect, in which case you do no
good at all, may make the OP waste valuable time on a
non-solution, and clutter up the group with OT-posts. As we now
know, your original response was of the latter variety.

Note that in a group where the response is on-topic, there are a
lot of people fluent and willing to respond in the case of
answers good and bad. Here, you may only get one answer, and it
very well might be wrong.

So, redirecting to an appropriate group is obviously a much
better path.
 
C

coder1024

Randy said:
Precisely. Such a solution is off-topic in this newsgroup, but
decidedly on-topic in newsgroups populated with people familiar
with his platform. Here's the conundrum. If you answer an
off-topic question in a newsgroup, there is the possibility that
you are correct, in which case you do some good. There is also
the possibility that you are incorrect, in which case you do no
good at all, may make the OP waste valuable time on a
non-solution, and clutter up the group with OT-posts. As we now
know, your original response was of the latter variety.

yes, there's always the possibility that a reply will not completely
solve the poster's problem or that it could be incorrect. thats
probably not a reason to avoid trying to provide some help. I've used
gettimeofday() a number of times and found it to work and felt it would
at least provide the OP with something to try. it would be a fairly
quick thing for the OP to check and see if they can use that function.
plus, there are probably a lot of readers who would be on platforms
where this is provided and so even if it didn't solve the OP problem it
could solve the next persons problem when searching for an answer.

at any rate, back to the OP's topic, I think something like APR would
probably be a good option.
So, redirecting to an appropriate group is obviously a much
better path.

the OP was provided with re-directions by other posters. I was trying
to provide a thread to pull which I felt might solve their problem.
 
D

Dominik Wallner

coder1024 said:
You might want to consider looking at the Apache Portable Runtime
project (http://apr.apache.org/). Since portability is a concern this
might provide a nice C library to handle your time needs and other
issues you may come across as you continue your development.

For the specific time issue you've posted about, see the apr_time_now()
function in their time functions module
(http://apr.apache.org/docs/apr/group__apr__time.html#ga6)

apr_time_t apr_time_now (void) ;

where apr_time_t is microseconds since epoch. this may be better than
gettimeofday() as I originally suggested, since APR runs on a very wide
variety of platforms, where it might be hit and miss wrt whether or not
your platform/environment provides gettimeofday().

Of course, this isn't part of the standard language, but the APR does
provide a very portable set of C functions and so might be of value to
you.

Thank you for that hints! I'll take a look at that, but as this seems to
be a rather big library (and I don't want that much dependencies of my
code) I may or may not find it useful.

Thank you anyway!
Yours,
Dominik Wallner
 
F

Flash Gordon

coder1024 said:
Randy Howard wrote:


the OP was provided with re-directions by other posters. I was trying
to provide a thread to pull which I felt might solve their problem.

The point is you have ABSOLUTELY NO CLUE as to what might be helpful to
the OP and your suggestion of gettimeofday I can say quite categorically
is of ABSOLUTELY NO HELP and so a COMPLETE waste of everyone's time.
Hence, if you actually know the platform you can point the poster in the
right direction AND tell them to go to a more appropriate group
(suggesting a solution without redirecting them is inappropriate), but
if you don't know the platform the ONLY thing you can sensibly do is
tell them to go to a more appropriate group.
 
M

Michael Mair

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

Indeed. Just got tired of preaching it all the time ;-)

-Michael
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top