C++ Question

I remember hearing about C++ code that, when placed in the program, calculated the total run-time of the program but cannot find it. What I want it for is to be able to test the efficiency of a program using different methods and such. Is there anyone out there that knows what I’m talking about?? Thanks in advance!

It wouldn’t be terribly difficult. Capture the system timer at the start of the program, and compare the stored value to the current system timer at each of the program’s exit points, and output the difference. That’s your run time.

There is a simple way to do this, however I do not know the specific commands. Essentially you do what Q.E.D. described. I wish I would have dug deeper in my programming class, but this was not one of the topics we covered.

That’s kind of what I thought QED. I guess I’ll just have to search around for the code because I’ve yet to learn how to do that. Thanks!

It depends on the kind of output you want. The output methods will be different depening if you are programming for a Windows or DOS environment. Basically it will look something like this for a Windows app:



int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPSTR lpCmdLine, int nCmdShow)
{
	TMyApp MyApp("Program_name", hInstance, hPrevInstance,
			lpCmdLine, nCmdShow);
     SetMessageQueue( 8 );
                **Get system time here**
 	MyApp.Run();
                **Return system time - saved system time here**
  	return MyApp.Status;
}


The only special variable is the system time, and you can get that from your compiler documentation.

#include <time.h>

time_t Start = time(NULL);

time_t End = time(NULL);

double DiffInSeconds = difftime(End, Start);

Or if you’re using Linux, you can just run your executable as an argument to /usr/bin/time. It will give you output like this:

time ./test

real 0m0.056s
user 0m0.025s
sys 0m0.025s

Joe_Cool’s method would also work in HP-UX and most other flavors of UNIX.

Or, if you’re in some flavor of Windows, the preferred function is GetTickCount(), as follows:

DWORD first_tick, last_tick, milliseconds_elapsed;

first_tick = GetTickCount();

// Run program code

last_tick = GetTickCount();

milliseconds_elapsed = last_tick - first_tick;

Don’t forget the high performance timers. Although I don’t have sample code for you, just do a search for it and I’m sure you’ll find some (suggested search terms {Google}: “High performance timer” optimize code). Also, consider looking around game programming forums for more information on optimizing code.

This is what I use in a program I happen to have up right now. This is only a little different than what ultrafilter has. I never had to use difftime to get the difference.

#include <time.h>

time_t ltime, ltime0;
time( &ltime0 );
// Stuff you want to time goes here.
time( &ltime );
cout << " Ellapsed time = " << ltime - ltime0 << " seconds." << endl;
You can call time( &ltime ); whenever you want a new reading on the ellapsed time.

Thanks everyone! You knew exactly what i was looking for!

To those who suggested code with the line #include <time.h> – please note that this is not standard C++. If it works with any given compiler, then it’s simply because that compiler supports this construction as a proprietary extension. The proper way of including the C time library in a C++ program is with the directive

#include <ctime>

Using this will ensure that your code works on any conformant C++ compiler.

Why not just run a profiler on it and get a bit more inofrmative output than just the time.

The ISO C++ standard, D.5.1, states:

and the table contains <time.h>. It’s a deprecated feature, and you can certainly argue that <ctime> is better style, but <time.h> is standard C++.

OK, yes, you’re technically right, MrNeutron, but including <time.h> instead of <ctime> may result in name clashes. If your program already defines a function time() in the default namespace, then using <time.h> will not work. (Of course, it goes without saying that using <ctime> mandates use of the std:: prefix when calling its functions.)