exact time

Discussion in 'C Programming' started by Henning Hasemann, May 23, 2006.

  1. Hi all,
    sounds trivial (I hope it is): Im searching for a way to get a
    time-value thats more exact than just seconds (i.e. I need milli- or
    microseconds or something similar, just at least as good as 1/100 second
    should suffice) and it should work with gcc (really g++) under linux and
    the MingW compliant under windows xp.

    I found an easy and universal sounding way in gmtime & co, but they dont
    seem to be available under my windows mingw installation.
    Is gmtime standard? Is my mingw installation broken?

    If it whould be too compilcated to find *one* way for both os' I whould
    be happy about two #ifdef'ed blocks of code, too.

    TIA
    Henning
     
    Henning Hasemann, May 23, 2006
    #1
    1. Advertising

  2. Henning Hasemann <> writes:
    > sounds trivial (I hope it is): Im searching for a way to get a
    > time-value thats more exact than just seconds (i.e. I need milli- or
    > microseconds or something similar, just at least as good as 1/100
    > second should suffice) and it should work with gcc (really g++) under
    > linux and the MingW compliant under windows xp.
    >
    > I found an easy and universal sounding way in gmtime & co, but they
    > dont seem to be available under my windows mingw installation.
    > Is gmtime standard? Is my mingw installation broken?
    >
    > If it whould be too compilcated to find *one* way for both os' I
    > whould be happy about two #ifdef'ed blocks of code, too.


    gmtime() is a standard C function; your mingw installation almost
    certainly supports it. It may be missing some other non-standard
    functions.

    There is no portable way to get a timestamp to any particular
    resolution; the C standard doesn't even guarantee 1-second resolution
    for time().

    See question 19.37 in the comp.lang.c FAQ, <http://www.c-faq.com/>.

    For more information, consult the documentation for the systems you're
    using and/or post to a system-specific newsgroup.

    <OFF_TOPIC><HINT>gettimeofday</HINT></OFF_TOPIC>

    --
    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, May 23, 2006
    #2
    1. Advertising

  3. Henning Hasemann

    Richard Bos Guest

    Henning Hasemann <> wrote:

    > sounds trivial (I hope it is): Im searching for a way to get a
    > time-value thats more exact than just seconds (i.e. I need milli- or
    > microseconds or something similar, just at least as good as 1/100 second
    > should suffice) and it should work with gcc (really g++) under linux and
    > the MingW compliant under windows xp.
    >
    > I found an easy and universal sounding way in gmtime & co, but they dont
    > seem to be available under my windows mingw installation.
    > Is gmtime standard? Is my mingw installation broken?


    gmtime() is ISO C. If your MingW doesn't have it, it's broken. FWIW, my
    installation Dev-C++ with MingW does have it. However, the ISO C part of
    gmtime() cannot possibly give you sub-second resolution, because there
    is no required member in the struct tm it creates smaller than an
    integral number of seconds (though it is allowed to add additional
    members, but those are then non-Standard and therefore non-portable);
    even a time_t is not guaranteed to have that small a resolution (though
    of course it's allowed to); in fact, there is no ISO C function that
    does give you this.

    Richard
     
    Richard Bos, May 23, 2006
    #3
  4. Henning Hasemann

    Flash Gordon Guest

    Henning Hasemann wrote:
    > Hi all,
    > sounds trivial (I hope it is): Im searching for a way to get a
    > time-value thats more exact than just seconds (i.e. I need milli- or
    > microseconds or something similar, just at least as good as 1/100 second
    > should suffice)


    Standard C provides no such mechanism.

    > and it should work with gcc (really g++)


    g++ is a C++ compiler, *not* a C compiler. C++ is a different language
    with it's own group.

    > under linux and
    > the MingW compliant under windows xp.
    >
    > I found an easy and universal sounding way in gmtime & co, but they dont


    It's hardly universal if it isn't supported everywhere!

    > seem to be available under my windows mingw installation.
    > Is gmtime standard? Is my mingw installation broken?
    >
    > If it whould be too compilcated to find *one* way for both os' I whould
    > be happy about two #ifdef'ed blocks of code, too.


    To get sub second resolution you will have to use OS specific mechanisms
    which almost certainly means you will have to use conditional compilation.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 23, 2006
    #4
  5. Henning Hasemann <> writes:

    >If it whould be too compilcated to find *one* way for both os' I whould
    >be happy about two #ifdef'ed blocks of code, too.


    <OT>
    Some code for Linux:


    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <sys/times.h>
    #include <sys/resource.h>

    static struct timeval startw, endw;
    static struct rusage startt, endt;

    static void time_taken(void)
    {
    printf("real=%ldms,",
    ((endw.tv_sec - startw.tv_sec)*1000 +
    (endw.tv_usec - startw.tv_usec)/1000));
    printf("usr=%ldms,",
    ((endt.ru_utime.tv_sec - startt.ru_utime.tv_sec)*1000 +
    (endt.ru_utime.tv_usec - startt.ru_utime.tv_usec)/1000));
    printf("sys=%ldms\n",
    ((endt.ru_stime.tv_sec - startt.ru_stime.tv_sec)*1000 +
    (endt.ru_stime.tv_usec - startt.ru_stime.tv_usec)/1000));
    }

    static void do_something(void)
    {
    int i;

    for(i=0 ; i<10000000 ; i++)
    getpid();
    }

    int main(int argc, char *argv[])
    {
    gettimeofday(&startw, NULL); getrusage(RUSAGE_SELF,&startt);

    do_something();

    gettimeofday(&endw, NULL); getrusage(RUSAGE_SELF,&endt);

    time_taken();

    return(0);
    }

    </OT>

    --
    Chris.
     
    Chris McDonald, May 23, 2006
    #5
  6. Chris McDonald <> writes:
    > Henning Hasemann <> writes:
    >
    >>If it whould be too compilcated to find *one* way for both os' I whould
    >>be happy about two #ifdef'ed blocks of code, too.

    >
    > <OT>
    > Some code for Linux:
    >
    >
    > #include <stdio.h>
    > #include <fcntl.h>
    > #include <sys/types.h>

    [snip]
    > </OT>


    Enclosing most of your article in <OT>...</OT> is not a license to
    post blatantly system-specific code.

    There are very good reasons *not* to post system-specific code in
    comp.lang.c. The people who are qualified to comment on it don't
    necessarily hang out here; they're in system-specific newsgroups.

    As far as I can tell, the code you posted should work on any Unix-like
    system, not just Linux; that means comp.unix.programmer is the correct
    place to discuss it. <OT>There's at least one error in the code;
    you'll see it if you compile it with warnings enabled.</OT>

    --
    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, May 23, 2006
    #6
  7. Keith Thompson <> writes:

    >Enclosing most of your article in <OT>...</OT> is not a license to
    >post blatantly system-specific code.


    Agreed, and that not withstanding ........, I haven't got the breath to
    start that debate again. I hope it was helpful to more that the OP.


    >As far as I can tell, the code you posted should work on any Unix-like
    >system, not just Linux; that means comp.unix.programmer is the correct
    >place to discuss it. <OT>There's at least one error in the code;
    >you'll see it if you compile it with warnings enabled.</OT>


    Thanks; found the problem.
    I bet a system-specific group wouldn't have highlighted that problem for me.
    A beneficial side-effect of doing the wrong thing.

    ----
    Chris.
     
    Chris McDonald, May 23, 2006
    #7
  8. Flash Gordon wrote:
    > Standard C provides no such mechanism.


    What a pitty. As I notices this is sensitivefor going into off-topic,
    (no personal statement regarding that atm), what whould be the right
    place to get my windows-solution? (If it is allowed to ask)

    > > and it should work with gcc (really g++)

    >
    > g++ is a C++ compiler, *not* a C compiler. C++ is a different language
    > with it's own group.


    Yes, I know. I Know the differences between C and C++. I read several
    books about C++ and enough about C to clearly see the difference.
    But the point is, that a solution in/for C will help me in 99% too as
    including c libraries in c++ is not a problem and I supposed the
    solution whould be something in time.h I didnt discover yet.

    > > under linux and
    >> the MingW compliant under windows xp.
    >>
    >> I found an easy and universal sounding way in gmtime & co, but they dont

    >
    > It's hardly universal if it isn't supported everywhere!


    Well, to answer a few other posts too: Not that sure is was gmtime.
    Reading Chris McDonalds posts brings me to the point my mind played
    tricks on me (happening often at the moment) and it was gettimeofday()
    which wasnt found.

    But I now see the point I will have to use something OS specific, thanks
    for that information to all. (Although Im not happy with that result)

    Henning
     
    Henning Hasemann, May 23, 2006
    #8
  9. Henning Hasemann

    Flash Gordon Guest

    Henning Hasemann wrote:
    > Flash Gordon wrote:
    >> Standard C provides no such mechanism.

    >
    > What a pitty. As I notices this is sensitivefor going into off-topic,
    > (no personal statement regarding that atm), what whould be the right
    > place to get my windows-solution? (If it is allowed to ask)


    Possibly microsoft.public.vc or somewhere in the
    comp.os-ms-windows.programmer hierarchy. Check the FAQ for the groups first.

    For information, asking if something is possible or how to do it is
    acceptable if you don't know it can't be done in standard C.

    >> > and it should work with gcc (really g++)

    >>
    >> g++ is a C++ compiler, *not* a C compiler. C++ is a different language
    >> with it's own group.

    >
    > Yes, I know. I Know the differences between C and C++. I read several
    > books about C++ and enough about C to clearly see the difference.
    > But the point is, that a solution in/for C will help me in 99% too as
    > including c libraries in c++ is not a problem and I supposed the
    > solution whould be something in time.h I didnt discover yet.


    OK, but C++ may have solutions available for some things that C does not
    and experts on comp.lang.c++ will know those parts of C that have been
    incorporated in to the C++ standard.

    <snip>

    > But I now see the point I will have to use something OS specific, thanks
    > for that information to all. (Although Im not happy with that result)


    Aiming for fully portable solutions is good, unfortunately not all
    problems can be solved portably.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc

    Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
     
    Flash Gordon, May 23, 2006
    #9
  10. Chris McDonald <> writes:
    > Keith Thompson <> writes:

    [...]
    >>As far as I can tell, the code you posted should work on any Unix-like
    >>system, not just Linux; that means comp.unix.programmer is the correct
    >>place to discuss it. <OT>There's at least one error in the code;
    >>you'll see it if you compile it with warnings enabled.</OT>

    >
    > Thanks; found the problem.
    > I bet a system-specific group wouldn't have highlighted that problem for me.
    > A beneficial side-effect of doing the wrong thing.


    I bet it would have. I hang out in comp.unix.programmer sometimes,
    and a subject of "exact time" probably would have caught my attention.

    --
    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, May 23, 2006
    #10
  11. Henning Hasemann <> writes:
    > Flash Gordon wrote:
    >> Standard C provides no such mechanism.

    >
    > What a pitty. As I notices this is sensitivefor going into off-topic,
    > (no personal statement regarding that atm), what whould be the right
    > place to get my windows-solution? (If it is allowed to ask)


    First, check your system's documentation. If that fails, I'd try a
    Google search. If that fails, post to a Windows-specific newsgroup;
    comp.os.ms-windows.programmer.win32 is a likely choice, or perhaps
    something under the microsoft.* hierarchy.

    --
    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, May 23, 2006
    #11
  12. Henning Hasemann

    CBFalconer Guest

    Flash Gordon wrote:
    >

    .... snip ...
    >
    > OK, but C++ may have solutions available for some things that C
    > does not and experts on comp.lang.c++ will know those parts of C
    > that have been incorporated in to the C++ standard.


    Anything that is soluble in C++ is soluble in C. It may require a
    tad more thinking. However the reverse does not apply.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, May 23, 2006
    #12
  13. Henning Hasemann

    Flash Gordon Guest

    CBFalconer wrote:
    > Flash Gordon wrote:
    > ... snip ...
    >> OK, but C++ may have solutions available for some things that C
    >> does not and experts on comp.lang.c++ will know those parts of C
    >> that have been incorporated in to the C++ standard.

    >
    > Anything that is soluble in C++ is soluble in C. It may require a
    > tad more thinking. However the reverse does not apply.


    It is still in general best to ask in a group about the language the
    compiler being used implements, and since the OP was using g++ rather
    than gcc that still suggests to me that comp.lang.c++ would have been a
    better place to ask.

    Anyway, they might one day add something to the C++ standard that cannot
    be done in standard C, only C-with-extensions, such as guaranteed
    sub-second timer resolution. ;-)
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 23, 2006
    #13
  14. In article <>,
    CBFalconer <> wrote:

    >Anything that is soluble in C++ is soluble in C. It may require a
    >tad more thinking. However the reverse does not apply.


    If the reverse does not apply, then how were the first generation
    C++ compilers actually C++ to C translators? ("cfront")

    Though if I recall, cfront did not have exceptions.

    I haven't looked at the C++ standard library in some time; possibly
    there are some operations that are well defined in it that are
    system dependant or system extensions in C ?
    --
    "law -- it's a commodity"
    -- Andrew Ryan (The Globe and Mail, 2005/11/26)
     
    Walter Roberson, May 23, 2006
    #14
  15. Henning Hasemann

    CBFalconer Guest

    Walter Roberson wrote:
    > CBFalconer <> wrote:
    >
    >> Anything that is soluble in C++ is soluble in C. It may require a
    >> tad more thinking. However the reverse does not apply.

    >
    > If the reverse does not apply, then how were the first generation
    > C++ compilers actually C++ to C translators? ("cfront")


    That was an example of using C to solve a C++ problem.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, May 24, 2006
    #15
    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. J
    Replies:
    1
    Views:
    303
    Kevin Spencer
    Aug 20, 2003
  2. wweric
    Replies:
    1
    Views:
    345
  3. Replies:
    3
    Views:
    701
  4. flamesrock
    Replies:
    8
    Views:
    509
    Hendrik van Rooyen
    Nov 24, 2006
  5. Jaco Naude

    Exact simulation time in ModelSim

    Jaco Naude, Sep 3, 2007, in forum: VHDL
    Replies:
    3
    Views:
    952
    HT-Lab
    Sep 3, 2007
Loading...

Share This Page