Measuring time differences

Discussion in 'C Programming' started by Dominik Wallner, Dec 16, 2005.

  1. 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
    Dominik Wallner, Dec 16, 2005
    #1
    1. Advertising

  2. Dominik Wallner

    coder1024 Guest

    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.
    coder1024, Dec 16, 2005
    #2
    1. Advertising

  3. "coder1024" <> 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.


    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.

    --
    Keith Thompson (The_Other_Keith) <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, Dec 16, 2005
    #3
  4. Dominik Wallner <> 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) <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, Dec 16, 2005
    #4
  5. Dominik Wallner

    Michael Mair Guest

    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.

    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
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Dec 16, 2005
    #5
  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.


    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.
    Martin Ambuhl, Dec 16, 2005
    #6
  7. Dominik Wallner

    Flash Gordon Guest

    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
    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).
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Dec 16, 2005
    #7
  8. Dominik Wallner

    Flash Gordon Guest

    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.
    Flash Gordon, Dec 16, 2005
    #8
  9. Dominik Wallner

    Chuck F. Guest

    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.

    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)
    Chuck F., Dec 16, 2005
    #9
  10. Dominik Wallner

    pete Guest

    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
    pete, Dec 16, 2005
    #10
  11. Dominik Wallner

    coder1024 Guest

    > 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.
    coder1024, Dec 17, 2005
    #11
  12. Dominik Wallner

    coder1024 Guest

    > 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.
    coder1024, Dec 17, 2005
    #12
  13. "coder1024" <> writes:
    >> 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.


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

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


    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.

    --
    Keith Thompson (The_Other_Keith) <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, Dec 17, 2005
    #13
  14. Dominik Wallner

    coder1024 Guest

    > 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.
    coder1024, Dec 17, 2005
    #14
  15. Dominik Wallner

    coder1024 Guest

    Keith Thompson wrote:
    > > 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.


    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 Thompson wrote:
    > >> 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.

    >
    > There isn't one.


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

    Keith Thompson wrote:
    > > 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.


    you're probably right. see my later reply. I think the OP could be
    well served by checking out the APR.
    coder1024, Dec 17, 2005
    #15
  16. Dominik Wallner

    Randy Howard Guest

    coder1024 wrote
    (in article
    <>):

    >
    > Keith Thompson wrote:
    >>> 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.

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


    >>> I'm not aware of a standard C function to provide what the OP was
    >>> looking for.

    >>
    >> There isn't one.

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

    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
    Randy Howard, Dec 17, 2005
    #16
  17. Dominik Wallner

    coder1024 Guest

    Randy Howard wrote:
    > 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.
    coder1024, Dec 17, 2005
    #17
  18. coder1024 wrote:
    >
    > 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
    Dominik Wallner, Dec 17, 2005
    #18
  19. Dominik Wallner

    Flash Gordon Guest

    coder1024 wrote:
    > Randy Howard wrote:


    <snip>

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


    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.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Dec 17, 2005
    #19
  20. Dominik Wallner

    Michael Mair Guest

    pete wrote:
    > 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.


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

    -Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Dec 17, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Raggio
    Replies:
    10
    Views:
    782
    rpadev
    Mar 7, 2004
  2. Wes Harrison

    Measuring a short interval in time

    Wes Harrison, Sep 14, 2004, in forum: Java
    Replies:
    2
    Views:
    567
    Paul Lutus
    Sep 14, 2004
  3. Jeremy Watts
    Replies:
    3
    Views:
    67,302
    Jeremy Watts
    Jun 14, 2005
  4. Home_Job_opportunity
    Replies:
    0
    Views:
    482
    Home_Job_opportunity
    Jan 8, 2009
  5. Home_Job_opportunity
    Replies:
    0
    Views:
    568
    Home_Job_opportunity
    Jan 14, 2009
Loading...

Share This Page