Thread: How to determine the execution time of a function (or an entire program)

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    36

    How to determine the execution time of a function (or an entire program)

    Hello everyone,
    I need to determine the time it took for a function to execute in a program that I have written
    I am using Windows 7 Ultimate
    My processor is Intel(R) Core(TM) i5 CPU 760 @2.80GHz 2.80GHz (got this from My Computer) with 4GB RAM
    Windows is 32 bit version
    (I'm sorry if all this information is irrelevant)

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I only know the GNU C library specific functions and haven't checked if they can be used on windows via MinGW, but I'm sure there is some native equivalent there. You want a function that will report the time at the level of milliseconds, and do something like this:

    Code:
    #include <stdio.h>
    #include <sys/timex.h>
    
    void reportTimeDif (struct ntptimeval *a, struct ntptimeval *b) {
    	long mic = b->time.tv_usec - a->time.tv_usec,
    		sec = b->time.tv_sec - a->time.tv_sec;
    
    	if (mic < 0) {
    		sec--;
    		mic *= -1;
    		mic++;
    	}
    
    	printf("Elapsed: %ld microseconds\n", mic + sec * 1000000);
    }
    
    #include <unistd.h> // for sleep call
    
    int main (void) {
    	struct ntptimeval start, end;
    
    	ntp_gettime(&start);
    	sleep(3);  // your function call here
    	ntp_gettime(&end);
    	reportTimeDif(&start, &end);
    
    	return 0;
    }
    Elapsed: 3000170 microseconds

    This is in microsecond because that's what ntptimeval uses, but you should aim to round that off to whole milliseconds. Ie, if your result was "562 microseconds", or 0.5 ms, don't trust that measurement, the scale is too small (the OS kernel is unlikely to report that accurately). Instead, execute the function over and over again in a for loop, measuring the for loop, and divide. For simple functions, you may have to execute thousands or hundreds of thousands of times.

    There are also tools called profilers that can report this kind of information, altho that is not their primary purpose and they may introduce inaccuracies with regard to the objective time because they slow execution down slightly, I believe (they are intended to compare relatively, ie, what percentage of the overall execution time is spent doing x y and z).
    Last edited by MK27; 03-06-2012 at 09:36 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    for Windows investigate QueryPerformanceCounter and QueryPerformanceFrequency to get high resolution time stamps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function execution time
    By DeanWinchester in forum C Programming
    Replies: 5
    Last Post: 01-08-2012, 02:26 PM
  2. measuring function execution time
    By nik in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2011, 09:21 PM
  3. How to get program execution time
    By pobri19 in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2009, 07:45 AM
  4. calcualte the execution time of a function
    By George2 in forum C Programming
    Replies: 2
    Last Post: 06-15-2006, 01:27 AM
  5. Windows 2k Dos- Program Execution Time
    By maththeorylvr in forum C Programming
    Replies: 1
    Last Post: 03-17-2005, 10:03 PM