A program that small will almost always run < 1s, unless there is resource contention on a multi-tasking OS.
I wrote a brief program a month or so ago that profiled the time required to compute an object's relativistic mass(1 sqrt, one division, 3 multiplies) 10,000 times. It ran well under a second.
You'll need a high resolution timer and the functionality is OS dependent. For Windows, look up QueryPerformanceFrequency and QueryPerformanceCounter.
For Linux look at clock.
If you're running Windows, add this to your program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// variables to add
_int64 countspersec, curr_time, prev_time;
double secpercount;
// Get the counts per second for the HR timer and calculate the number of seconds one count corresponds to.
QueryPerformanceFrequency((LARGE_INTEGER*)&countspersec);
secpercount = 1.0 / (double)countspersec;
// sandwich your code between these two calls
QueryPerformanceCounter((LARGE_INTEGER*)&prev_time);
QueryPerformanceCounter((LARGE_INTEGER*)&curr_time);
// reporting code
cout << "Counts it took to run: " << curr_time - prev_time << endl;
cout << "Seconds it took to run: " << (curr_time - prev_time) * secpercount << endl;
I did as instructed and I had to add a windows header file. I got a ton of errors when I ran it. I working on a system with Windows 7 and using VS 2010.
// sandwich your code between these two calls
QueryPerformanceCounter((LARGE_INTEGER*)&prev_time);
QueryPerformanceCounter((LARGE_INTEGER*)&curr_time);
Which tells me it's not recognizing ULONG or PTR as valid typedefs. The online doc at MS says the functions are defined in WinBase.h, but that you should include windows.h.
Thanks guys. The windows.h header file did the trick.
The output is ridiculous:
0 1 2 3 4 5 6 7 8
Counts it took to run: 8512
Seconds it took to run: 0.00344921.
#include <Windows.h>
#include <iostream>
#include <cmath>
#include <iostream>
usingnamespace std;
int main( int argc, char *argv[] )
{
// variables to add
_int64 countspersec, curr_time, prev_time;
double secpercount;
double rel_mass, rest_mass = 5000000000, v = 100000, c = 299792458;
// Get the counts per second for the HR timer and calculate the number of seconds one count corresponds to.
QueryPerformanceFrequency((LARGE_INTEGER*)&countspersec);
secpercount = 1.0 / (double)countspersec;
// sandwich your code between these two calls
QueryPerformanceCounter((LARGE_INTEGER*)&prev_time);
for( int i = 0; i < 10000; i++ )
rel_mass = sqrt( rest_mass/(sqrt(1-(v*v)/(c*c))));
QueryPerformanceCounter((LARGE_INTEGER*)&curr_time);
// reporting code
cout << "Counts it took to run: " << curr_time - prev_time << endl;
cout << "Seconds it took to run: " << (curr_time - prev_time) * secpercount << endl;
cin.get();
}
Counts it took to run: 788
Seconds it took to run: 0.000268996
And I even have the equation wrong (superfluous sqrt in there).