wall clock time in milliseconds to time c code sections

Discussion in 'C Programming' started by pedro.ballester@gmail.com, Nov 18, 2006.

  1. Guest

    Hi everyone,

    I am struggling with the following problem. I would like to measure the
    wall clock time required to run a section of the code with a precision
    of milliseconds. The attached code does the job on Windows Xp. However,
    the code is not portable, as it fails to compile on Linux Red Hat. This
    is the command and the errors:

    # gcc -c time_loop.c
    time_loop.c: In function 'main':
    time_loop.c:34: error: storage size of 'tstruct1' isn't known
    time_loop.c:34: error: storage size of 'tstruct2' isn't known
    time_loop.c:26: warning: return type of 'main' is not 'int'

    I will be grateful if anyone could tell know how to fix this problem so
    that I can use the same code both on WinXp and Linux. In case it is not
    possible, it will be very helpful to have alternative, preferably
    concise, code that works on Linux.

    Thanks in advance,

    Pedro

    Ps: and this is time_loop.c

    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/timeb.h>

    /* GLOBAL PARAMETERS DECLARATION */

    #define NM 25000000

    /* MAIN ROUTINE */

    void main(void)
    {
    int i;
    long idb; /* with "register", it seems to take longer! */
    /* register long idb; */
    double tms1, tms2, telapsed;
    float diff[12];
    float vec_a[12]={6., 3., 4., 3., 7., 2., 1., 3., 8., 4., 9., 3.};
    float vec_b[12]={3., 7., 2., 1., 3., 8., 4., 9., 6., 3., 4., 3.};
    struct __timeb64 tstruct1, tstruct2;


    /* Starting time */
    _ftime64( &tstruct1 );
    tms1=(double)tstruct1.time+((double)tstruct1.millitm/1000.0);


    for(idb=0; idb < NM; idb++)
    {
    for(i=0; i < 12; i++) diff=vec_a-vec_a;
    }


    /* Finishing time */
    _ftime64( &tstruct2 );
    tms2=(double)tstruct2.time+((double)tstruct2.millitm/1000.0);
    telapsed=tms2-tms1;


    printf("It took:\t%lf seconds to calculate diff %d times\n",
    telapsed, NM );

    }
     
    , Nov 18, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi everyone,
    >
    > I am struggling with the following problem. I would like to measure the
    > wall clock time required to run a section of the code with a precision
    > of milliseconds. The attached code does the job on Windows Xp. However,
    > the code is not portable, as it fails to compile on Linux Red Hat. This
    > is the command and the errors:
    >
    > # gcc -c time_loop.c
    > time_loop.c: In function 'main':
    > time_loop.c:34: error: storage size of 'tstruct1' isn't known
    > time_loop.c:34: error: storage size of 'tstruct2' isn't known
    > time_loop.c:26: warning: return type of 'main' is not 'int'
    >
    > I will be grateful if anyone could tell know how to fix this problem so
    > that I can use the same code both on WinXp and Linux. In case it is not
    > possible, it will be very helpful to have alternative, preferably
    > concise, code that works on Linux.


    Standard C does not offer a way to measure milliseconds
    therefore I don't think there is a way to have such code
    which works both on Linux and Windows. For Linux specific
    advice you should ask at comp.os.linux.development.apps

    But there is a way to fix the following warning on both systems
    time_loop.c:26: warning: return type of 'main' is not 'int'
    I'll leave it as an exercise to find how.
     
    Spiros Bousbouras, Nov 18, 2006
    #2
    1. Advertising

  3. > wrote:
    > > time_loop.c:26: warning: return type of 'main' is not 'int'


    If this is the warning your compiler gave you it's
    either broken or the people who wrote it have a
    very strange sense of humor. But I suspect that
    you simply didn't copy the warning correctly. Cut
    and paste is the only reliable method for programmes
    and compiler messages.
     
    Spiros Bousbouras, Nov 18, 2006
    #3
  4. "Spiros Bousbouras" <> writes:
    >> wrote:
    >> > time_loop.c:26: warning: return type of 'main' is not 'int'

    >
    > If this is the warning your compiler gave you it's
    > either broken or the people who wrote it have a
    > very strange sense of humor. But I suspect that
    > you simply didn't copy the warning correctly. Cut
    > and paste is the only reliable method for programmes
    > and compiler messages.


    What do you mean? The OP's program had:

    void main(void)
    {
    ...
    }

    The warning seems perfectly correct; he defined a function called
    "main" whose return type is not int.

    And yes, that's the exact warning message I get from gcc for
    "void main(void)".

    The line number doesn't match the code he posted, but that indicates a
    copy-and-paste error for the code, not for the warning.

    --
    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, Nov 18, 2006
    #4
  5. Keith Thompson wrote:
    > "Spiros Bousbouras" <> writes:
    > >> wrote:
    > >> > time_loop.c:26: warning: return type of 'main' is not 'int'

    > >
    > > If this is the warning your compiler gave you it's
    > > either broken or the people who wrote it have a
    > > very strange sense of humor. But I suspect that
    > > you simply didn't copy the warning correctly. Cut
    > > and paste is the only reliable method for programmes
    > > and compiler messages.

    >
    > What do you mean?


    My mind worked strangely. I understood
    "return type of 'main' is not 'int'"
    not as referring to main as appears in the
    programme but as saying that the return
    value of main should not be int.

    The warning is indeed correct.
     
    Spiros Bousbouras, Nov 18, 2006
    #5
  6. writes:

    > Hi everyone,
    >
    > I am struggling with the following problem. I would like to measure the
    > wall clock time required to run a section of the code with a precision
    > of milliseconds.

    <snip>
    > I will be grateful if anyone could tell know how to fix this problem so
    > that I can use the same code both on WinXp and Linux. In case it is not
    > possible, it will be very helpful to have alternative, preferably
    > concise, code that works on Linux.


    This can't be done in standard (and hence portable) C. In such cases
    it is often best to go the Unix(ish) route and use a function
    specified in the POSIX standard since there are POSIX C libraries for
    a lot of systems (including MS ones).

    <offtopic>The POSIX function you want is gettimeofday. More
    information and code examples would probably follow if you post on
    comp.unix.programmer.</offtopic>

    --
    Ben.
     
    Ben Bacarisse, Nov 19, 2006
    #6
    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. Peter Hansen
    Replies:
    13
    Views:
    707
    Peter Hansen
    Jul 7, 2005
  2. Coates, Steve (ACHE)

    RE: Determining actual elapsed (wall-clock) time

    Coates, Steve (ACHE), Jul 4, 2005, in forum: Python
    Replies:
    2
    Views:
    406
    Roy Smith
    Jul 4, 2005
  3. Geert Jansen
    Replies:
    1
    Views:
    337
    Dieter Maurer
    Nov 26, 2005
  4. aegis

    wall clock time

    aegis, Dec 15, 2004, in forum: C Programming
    Replies:
    12
    Views:
    675
    infobahn
    Dec 18, 2004
  5. Raman
    Replies:
    6
    Views:
    4,844
    santosh
    Aug 3, 2007
Loading...

Share This Page