timing how long  functions/routine

Posted on 2007-08-02
Last Modified: 2010-04-01
Hi folks, I want to time particular function calls in my routine(s). For example, I remove color in one program version by an iterating routine. In another version I do it by calling a function that does a matrix transform with a LUT table. But the bottom line is, I need to evaluate which is faster.I also don't mind if there are program breaks along the way that return the time. Thats is unimportant as I will eventually take it back out.  Is there a quick a dirty way to do this?

Question by:Wanderinglazyeye
    LVL 30

    Expert Comment

    What's your OS?
    LVL 30

    Expert Comment

    >>Is there a quick a dirty way to do this?

    For Win32, you can use GetTickCount()
    LVL 86

    Expert Comment

    A portable way to do that is using 'clock()', e.g.

    /* CLOCK.C: This example prompts for how long
     * the program is to run and then continuously
     * displays the elapsed time for that period.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    void sleep( clock_t wait );

    void main( void )
       long    i = 600000L;
       clock_t start, finish;
       double  duration;

       /* Delay for a specified time. */
       printf( "Delay for three seconds\n" );
       sleep( (clock_t)3 * CLOCKS_PER_SEC );
       printf( "Done!\n" );

       /* Measure the duration of an event. */
       printf( "Time to do %ld empty loops is ", i );
       start = clock();
       while( i-- )
       finish = clock();
       duration = (double)(finish - start) / CLOCKS_PER_SEC;
       printf( "%2.1f seconds\n", duration );

    /* Pauses for a specified number of milliseconds. */
    void sleep( clock_t wait )
       clock_t goal;
       goal = wait + clock();
       while( goal > clock() )
    LVL 30

    Accepted Solution



    DWORD StartTime = GetTickCount();

    //Do some work here

    LenTime = GetTickCount() - StartTime;

    printf("Time duration is %li\n", LenTime);
    LVL 86

    Assisted Solution

    BTW, if you are on Windows 'GetTickCount()' ( is a little more convenient, i.e.

    DWORD dwStart, dwEnd, dwDuration;

    dwStart = GetTickCount();

    for (int i = 0; i < 200000;++i) {

      // some calculation

    dwEnd = GetTickCount():

    dwDuration = dwEnd - dwStart;
    LVL 86

    Expert Comment

    Oh, and here's a nice and simple utility class for that task: ("Precise duration measurement -
    A simple class that provides high precision timing")
    LVL 9

    Assisted Solution

    There are tools that do this for you if you want something a little more sophisticated.   One that works fairly well for C++ is Rational's PurifyPlus: (it produces tables/charts and annotated source code that show you exactly where the time is being used in the execution of code).

    The tool is fairly expensive to buy - but you can download it and use it for free for 15 days.

    Author Comment

    Awesome everyone, thankyou. FYI I used axter/jkr's tick marks calls and produced some interesting results. I also may look at jasonclarke's suggestion as well.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now