what's the best way to time your code?

Discussion in 'C Programming' started by Pushkar Pradhan, Sep 28, 2003.

  1. I need to time my blocked matrix multiply code to decide the optimal
    block size.
    I know two ways to do it: time() and clock() functions.
    In order to get time is usec clock() must be divided by some CONSTANT
    which defines the no. of clocks per sec.

    Which one is more accurate or is there another best way?
    Pushkar
     
    Pushkar Pradhan, Sep 28, 2003
    #1
    1. Advertising

  2. Pushkar Pradhan

    pete Guest

    Pushkar Pradhan wrote:
    >
    > I need to time my blocked matrix multiply code to decide the optimal
    > block size.
    > I know two ways to do it: time() and clock() functions.
    > In order to get time is usec clock() must be divided by some CONSTANT
    > which defines the no. of clocks per sec.


    clock() is for timing code.

    --
    pete
     
    pete, Sep 28, 2003
    #2
    1. Advertising

  3. Il Sat, 27 Sep 2003 20:00:06 -0700, Pushkar Pradhan ha scritto:

    > Which one is more accurate or is there another best way?


    I add my little suggestion: if you're on unix (I'm thinking of Linux, but
    I suppose there are equivalents), remember the "time" command:

    mat@jericho:~/prog/ocaml/it$ time ./make_sign < parole.txt > parole_signed.txt

    real 0m11.593s
    user 0m9.390s
    sys 0m1.310s

    it can be very useful, sometimes, to get infos about true time of
    computation ("user" time). I do not know how much accurate is, but it
    seemed to me fairly enough.

    --
    /**
    * Mattia Belletti - Undergraduate student @ cs.unibo.it
    * ICQ: 33292311 - email:
    * IRC: BluShine - site(s): http://cs.unibo.it/~mbellett
    * Linux registered user 299762 @ Linux registered machine 213003
    */
     
    Mattia Belletti, Sep 28, 2003
    #3
  4. Pushkar Pradhan wrote:

    > I need to time my blocked matrix multiply code to decide the optimal
    > block size.
    > I know two ways to do it: time() and clock() functions.
    > In order to get time is usec clock() must be divided by some CONSTANT
    > which defines the no. of clocks per sec.
    >
    > Which one is more accurate or is there another best way?
    > Pushkar


    Either function is not accurate enough to time a single
    pass. The most accurate method is to use a platform specific
    operating system call.

    Otherwise, you would have to run enough iterations through your
    code so you can use the clock() or time() functions.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Sep 29, 2003
    #4
  5. Pushkar Pradhan

    Dan Pop Guest

    In <> Mattia Belletti <> writes:

    >Il Sat, 27 Sep 2003 20:00:06 -0700, Pushkar Pradhan ha scritto:
    >
    >> Which one is more accurate or is there another best way?

    >
    >I add my little suggestion: if you're on unix (I'm thinking of Linux, but
    >I suppose there are equivalents), remember the "time" command:
    >
    >mat@jericho:~/prog/ocaml/it$ time ./make_sign < parole.txt > parole_signed.txt
    >
    >real 0m11.593s
    >user 0m9.390s
    >sys 0m1.310s
    >
    >it can be very useful, sometimes, to get infos about true time of
    >computation ("user" time). I do not know how much accurate is, but it
    >seemed to me fairly enough.


    You forgot to mention how this can be used to time only a certain part
    of the program's code.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 29, 2003
    #5
  6. Pushkar Pradhan

    Dan Pop Guest

    In <> (Pushkar Pradhan) writes:

    >I need to time my blocked matrix multiply code to decide the optimal
    >block size.
    >I know two ways to do it: time() and clock() functions.
    >In order to get time is usec clock() must be divided by some CONSTANT
    >which defines the no. of clocks per sec.
    >
    >Which one is more accurate or is there another best way?


    On most platforms, for most code timing purposes, time() is useless: it
    doesn't tell you anything about how much time the CPU spent working on
    your program.

    clock() is the right function and CLOCKS_PER_SEC is the constant name.
    Note, however, that despite the CLOCKS_PER_SEC value, the real resolution
    of the clock() function is seldom less than 10 millisec. So, don't try
    to use it on pieces of code that run in less than 1 CPU second.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 29, 2003
    #6
  7. Pushkar Pradhan

    pete Guest

    Dan Pop wrote:
    >
    > In <> (Pushkar Pradhan) writes:
    >
    > >I need to time my blocked matrix multiply code to decide the optimal
    > >block size.
    > >I know two ways to do it: time() and clock() functions.
    > >In order to get time is usec clock() must be divided by some CONSTANT
    > >which defines the no. of clocks per sec.
    > >
    > >Which one is more accurate or is there another best way?

    >
    > On most platforms, for most code timing purposes, time() is useless: it
    > doesn't tell you anything about how much time the CPU spent working on
    > your program.
    >
    > clock() is the right function and CLOCKS_PER_SEC is the constant name.
    > Note, however, that despite the CLOCKS_PER_SEC value,
    > the real resolution
    > of the clock() function is seldom less than 10 millisec.
    > So, don't try
    > to use it on pieces of code that run in less than 1 CPU second.


    I put fast test code in a loop that runs many times,
    time the loops with the test code,
    time the loops without the test code,
    and then divide the difference in time,
    by the number of loop cycles.

    --
    pete
     
    pete, Sep 30, 2003
    #7
  8. Pushkar Pradhan

    Eric Sosman Guest

    pete wrote:
    >
    > I put fast test code in a loop that runs many times,
    > time the loops with the test code,
    > time the loops without the test code,
    > and then divide the difference in time,
    > by the number of loop cycles.


    This is a good approach, but requires some care to avoid
    contamination by compiler optimizations. For example,

    t0 = clock();
    for (i = 0; i < 1000000; ++i) {
    #if INCLUDE_THE_CODE
    /* code being timed goes here */
    #endif
    }
    t1 = clock();

    .... may not work very well when INCLUDE_THE_CODE isn't defined,
    because the compiler may optimize the whole loop away and replace
    it with `i = 1000000;' -- or even with nothing at all, if `i'
    isn't used subsequently.

    A method that's worked for me is to use function pointers:

    void no_op(void) {}

    void for_real(void) {
    /* code being timed goes here */
    }

    int main(int argc, char **argv) {
    void (*func)(void) = (argc > 1) ? for_real : no_op;
    ...
    t0 = clock();
    for (i = 0; i < 1000000; ++i)
    func();
    t1 = clock();
    ...

    .... so the compiler cannot predict which function the loop will
    call. For an even greater comfort level, put the two called
    functions in a different module and compile it separately from
    the main program.

    Someday, perhaps, compilers and linkers will become smart
    enough to make even this technique fail (if anyone knows of an
    implementation this smart, I'm sure we'd like to hear about it).
    For the moment, though, it seems fairly robust.

    --
     
    Eric Sosman, Sep 30, 2003
    #8
  9. Pushkar Pradhan

    pete Guest

    Eric Sosman wrote:
    >
    > pete wrote:
    > >
    > > I put fast test code in a loop that runs many times,
    > > time the loops with the test code,
    > > time the loops without the test code,
    > > and then divide the difference in time,
    > > by the number of loop cycles.

    >
    > This is a good approach, but requires some care to avoid
    > contamination by compiler optimizations. For example,
    >
    > t0 = clock();
    > for (i = 0; i < 1000000; ++i) {
    > #if INCLUDE_THE_CODE
    > /* code being timed goes here */
    > #endif
    > }
    > t1 = clock();
    >
    > ... may not work very well when INCLUDE_THE_CODE isn't defined,
    > because the compiler may optimize the whole loop away and replace
    > it with `i = 1000000;' -- or even with nothing at all, if `i'
    > isn't used subsequently.
    >
    > A method that's worked for me is to use function pointers:
    >
    > void no_op(void) {}


    I do that too.

    assert(p_l[0].f == nosort);

    func_time = loop_time = doit(nosort, s2, s3, s1, N, 0);
    for (function = 1; FUNCTIONS > function; ++function) {
    func_time
    = doit(p_l[function].f, s2, s3, s1, N, function)
    - loop_time;
    p_l[function].t += func_time;
    if (func_time > TIME_MAX) {
    break;
    }
    if (func_time > func_time_max) {
    func_time_max = func_time;
    }
    }

    --
    pete
     
    pete, Oct 1, 2003
    #9
  10. Pushkar Pradhan

    CBFalconer Guest

    pete wrote:
    > Eric Sosman wrote:
    > >

    .... snip ...
    > >
    > > A method that's worked for me is to use function pointers:
    > >
    > > void no_op(void) {}

    >
    > I do that too.


    I suggest hiring some of Maxwells demons. When not busy timing
    code they can be put to their original use, thus creating fuelless
    heat engines and/or refrigerators.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 1, 2003
    #10
    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. flamesrock
    Replies:
    8
    Views:
    466
    Hendrik van Rooyen
    Nov 24, 2006
  2. jonny
    Replies:
    1
    Views:
    439
    David R. Longnecker
    Oct 10, 2007
  3. Your Encryption Site
    Replies:
    11
    Views:
    711
    Kim André Akerø
    Apr 24, 2008
  4. Paul Rubin
    Replies:
    5
    Views:
    417
    Hendrik van Rooyen
    Aug 6, 2009
  5. Brent
    Replies:
    0
    Views:
    120
    Brent
    Dec 26, 2003
Loading...

Share This Page