Thread: Function execution time

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Function execution time

    Hey guys,
    do you know how to check a function execution time ?

    Thanks,
    Dean.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    By having it execute tens of thousands of times in a very tight loop while tracking start time and stop time.

    I don't know if you're on Windows or one of the *nix flavours but on Windows it goes like this...

    Code:
    start = GetTickCount();  // in milliseconds
    for(int x = 0; x < 1000000; x++)
       MyFunction();
    elapsed = GetTickCount() - start;
    The catch is that unless you've written some horifficly complex function with all kinds of built in loops, access to hardware or other time consuming bits and pieces, execution time for a typical function is in microseconds, well below the timer resolution in most desktop computers.

    I recently did a small program to compare the speeds of stack allocation and heap allocation... It took more than 10,000,000 iterations of the two functions to expose any noticeable difference in speed... It took 10,000 iterations just to get it to show up on the tick counter.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by CommonTater View Post
    By having it execute tens of thousands of times in a very tight loop while tracking start time and stop time.

    I don't know if you're on Windows or one of the *nix flavours but on Windows it goes like this...

    Code:
    start = GetTickCount();  // in milliseconds
    for(int x = 0; x < 1000000; x++)
       MyFunction();
    elapsed = GetTickCount() - start;
    The catch is that unless you've written some horifficly complex function with all kinds of built in loops, access to hardware or other time consuming bits and pieces, execution time for a typical function is in microseconds, well below the timer resolution in most desktop computers.

    I recently did a small program to compare the speeds of stack allocation and heap allocation... It took more than 10,000,000 iterations of the two functions to expose any noticeable difference in speed... It took 10,000 iterations just to get it to show up on the tick counter.
    I'm on Ubuntu corrently and my program is really big, working with pointers, structures, everything.
    Any help working with that type of data ?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Look up your Compiler and function profile or profiling.

    Example result link: An Introduction to GCC - Using the profiler gprof

    Tim S.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Sorry... not a Linux programmer... not even a Linux user.

    However, I would advise against what the gang here calls "premature optimization"... That is, unless you can track a specific problem to something taking too long, you should spend your time and effort writing good and safe code. When unnecessary, optimization is usually nothing but a frustration...

    Necessary optimizations may come when (for example) you have high network traffic and are getting buffer full errors. There, you might have something that takes longer to execute than it takes to fill the network buffer... now you get busy finding out what's slowing you down.

    The rest of the time, just get it right... Nothing else matters.

    As for "pointers, structures and everything"... when you have control over the data structure there's a simple rule "KIS... Keep It Simple". While it won't make any difference in speed, your code will become infinitely easier to maintain if you elminate such common boondoggles as nested structs and pointers to pointers wherever you can. 9 of 10 times when I see someone writing berzerker lines like ...
    Code:
    day = data -> purchase -> order -> time -> day
    ... the first thing I figure is that their data probably needs reorganizing.

    Code should be written with a simple question in mind:
    "If I come back to this in 5 years, will I still be able to follow it?"

    If the answer is no... start over.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. measuring function execution time
    By nik in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2011, 09:21 PM
  2. How to get program execution time
    By pobri19 in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2009, 07:45 AM
  3. Comparing time taken for execution
    By AvaGodess in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 02:15 PM
  4. execution time
    By shuo in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2007, 02:58 AM
  5. calcualte the execution time of a function
    By George2 in forum C Programming
    Replies: 2
    Last Post: 06-15-2006, 01:27 AM