Thread: Timing a function

  1. #1
    Insomniac
    Join Date
    Mar 2004
    Posts
    35

    Timing a function

    Exercise 2-9 in the knr book asks for a rewrite of an example function, with my version being faster.
    I have written one, and by code alone, I deem it faster.
    My question is, how can I test the speed of a function using the tools given up to the exercise and no further?
    I have thought about feeding the function a large amount of values to process, and to manually time it myself in seconds, though it seems inappropriate.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    One way is to use time(). Only problem is that it will only give you it in seconds. For anything else it becomes compiler/OS specific.

    Code:
    unsigned int before, after;
    before = time(NULL);
    foo();
    after = time(NULL);
    printf("It took %ld seconds to complete the function\n", after-before);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That's about as much as you can do "by the book"

    To do anything more objective, you would need to call either clock() or time()

    And to get any kind of accuracy, you would need to call your test code anywhere between several hundred to several million times, depending how quick it is to start with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    Thanks to you both.
    Firstly, Thantos thank you for the code, I chose a different method to calculate the seconds, for anything mroet han seconds, I could always choose to perform the mathematics inside the actual program without calling on another other libs though, could i not

    Salem, thanks for the clock tip, I also made a few hundred million calls to the function.

    My time:
    wall clock time: 20
    CPU clock time: 19.570000
    KnR's time:
    wall clock time: 25
    CPU clock time: 24.160000
    I'm pretty happy with both functions. Thanks again

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    5
    hmm, you can always use GetTickCount() (This function retrieves the number of milliseconds the system has been running, excluding any time that the system was suspended.) ('windows.h'/'winbase.h').. If your on win32 of c.
    Simple example (not tested):
    Code:
    unsigned int before, after;
    before = GetTickCount();
    foo();
    after = GetTickCount();
    printf("Your function used %ld ms or %ld sec to succesfully complete.\n", before-after, (before-after)/60);
    // sPiKie
    I am new btw, nice to see you guys I have moved from VB to C\C++ last months on school

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am new btw, nice to see you guys
    Welcome. Just so you know, unless the OP claims to use a certain system we try to stick with portable solutions.

    >printf("Your function used %ld ms or %ld sec to succesfully complete.\n", before-after, (before-after)/60);
    You declared before and after as unsigned int yet tell printf that expressions using them result in long? I think you wanted %u instead of %ld.
    My best code is written with the delete key.

  7. #7
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    To go with what Prelude mentioned, I'm using bsd, so no win32 here.

    Also, welcome Thanks for the alternate method though

  8. #8
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by Thantos
    For anything else it becomes compiler/OS specific.
    What is wrong with clock()?
    The one who says it cannot be done should never interrupt the one who is doing it.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    clock() just like time() are only going to give you an approximation.
    To give a more accurate you have to use a non standard solution, thats what I was trying to say.

    Edit: Another method (again not extremely accurate) is to put it into a program all by itself and then call that program with time.
    Code:
    void foo (float , int );
    int main (void)
    {
      register unsigned int c;
      for (c=0; c<UINT_MAX; c++)
        foo (4.5837, c);
      return 0;
    }
    
    void foo (float x, int y)
    {
      /* Do something */
    }
    ie: time ./myprog

    Sit back and watch a movie because it's gonna take a while to do that function 4 billion times

    time gives output such as
    real 0m0.014s
    user 0m0.000s
    sys 0m0.000s

    Just a thought on another method
    Last edited by Thantos; 03-28-2004 at 08:12 PM.

  10. #10
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    Instead of subtracting the current time from the past time, I used difftime(x,y), which gave me the same result, but seems alot smarter

    Calling the program by time is more accurate than previous, is this due to the time_t data type? I tried using a double instead and the result was identical, just with trailing zeros.

    One noticeable difference between time ./program and timing the actual function, is that the time output is corupt. It's timing the program not the function, because of this the CPU time seems off.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > One noticeable difference between time ./program and timing the actual function
    Yeah, the time from the command line probably includes a lot of system type stuff (like the time to fork the process in the first place)

    > I used difftime(x,y), which gave me the same result, but seems alot smarter
    It's also much safer in the long run, since it always returns seconds. time_t typically stores seconds, but it doesn't have to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If you want true accuracy, use a profiler...

    GNU Tools: gprof
    MSVC: profiler
    Sun CC: lprof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Timing and Ending a Function
    By mikeman118 in forum Windows Programming
    Replies: 5
    Last Post: 12-20-2007, 11:16 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM