Thread: Timing in C/C++

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Timing in C/C++

    Hello,

    I would be very grateful for some help. I have a Visual C++ program and wish to time some critical sections of it. I followed the instructions by the MSDN library, but just get elapsed times of 0 or 1 second, so obviously something is wrong.

    I've been doing the following:

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

    ...........................

    time_t start,finish;
    double elapsed_time;

    ..........................

    time(&start);
    ....................
    // Lots of calculations
    ....................
    time(&finish);
    elapsed_time=difftime(finish,start);
    fprintf(fpoutput,"Elapsed = %10.6f seconds\n",elapsed_time);

    All I get printed in the output file is mostly 0 seconds, but occasionally 1 second, which makes no sense at all.

    I also linked in the library LIBC.LIB, but this made no difference. Someone's help in this would be most appreciated - thank you.

    Christopher Sharp

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A second is a seriously long time for most short programs

    Normally, you have to stretch time out, by doing something like
    Code:
    time(&start);
    for ( int i = 0 ; i < 1000 ; i++ ) {
        // Lots of calculations
    }
    time(&finish);
    elapsed_time=difftime(finish,start);
    fprintf(fpoutput,"Elapsed = %10.6f seconds\n",elapsed_time / 1000 );
    But this assumes your calcs can be repeated without affecting the results.

    There is also a windows specific function, which I think begins with 'getProfile', which reads the v.fast clock on the processor.
    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.

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you need a high resolution timer then you'll need to use QueryPerformanceFrequency() and QueryPerformanceCounter() -

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    
    	
    	_LARGE_INTEGER start,end,result,freq;
    	int i=0; 
    
    	if(!QueryPerformanceFrequency(&freq))
    		return 0;
    	
    	result.QuadPart=0;
    	QueryPerformanceCounter(&start);
    
    	while (i++<1000)
    		cout << "hello"<<endl;
    		
    	QueryPerformanceCounter(&end);
    	result.QuadPart=end.QuadPart-start.QuadPart;
    	cout <<(double)result.QuadPart/freq.QuadPart;
    	
    	return 0;
    }
    zen

  4. #4
    Registered User morbuz's Avatar
    Join Date
    Aug 2001
    Posts
    35
    Or you could use GetTickCount() which returns milliseconds since windows was last rebooted:

    Code:
    #include <windows.h>
    #include <iostream.h>
    
    int main()
    {
    unsigned int start, end;
    
    start = GetTickCount();
    // do stuff here
    end = GetTickCount();
    
    cout << "That took " << end-start << " milliseconds\n";
    
    return(0);
    }
    [Signature here. (Remove this!)]

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    2
    OK, many thanks indeed for the various helpful messages. I tried the last one by morbuz, as it is the simplest, and it works. A resolution of a millisecond is good enough for my purposes, as I have some critical sections of code with lots of number crunching, and I wanted to get a rough idea of how long it was taking.

    I'm still puzzeled why the first method I tried doesn't work. I'm using Visual C++6 with Windows 95, and the program is a Windows program without MFC.

    Christopher Sharp

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I'm still puzzeled why the first method I tried doesn't work.
    As Salem has stated, your program is too fast for the precision obtained by using time(). It only returns the whole seconds that have passed. So if no whole seconds have passed in between your calls before and after your code then time() will return the same value and difftime() will return 0.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Performance Timing Function
    By rosicky2005 in forum C++ Programming
    Replies: 11
    Last Post: 05-31-2007, 03:09 PM
  2. More precise timing
    By peeps in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2006, 11:22 PM
  3. My Timing System
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 01-01-2006, 11:43 PM
  4. Games - timing
    By Magos in forum Game Programming
    Replies: 7
    Last Post: 03-06-2004, 11:32 AM
  5. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 12:43 AM