Hi all,

For my project I'm doing a comparison between solving ODE's in MATLAB and using the GPU to solve them. Anyone know of an accurate way of timing how long the program takes to run on the gpu (same kind of accuracy provided by matlabs tic toc command)?

I have searched the forums and clock() apparently is not that accurate, another method depended on which compiler you were using and one person talk about using the profiler (?).

Recommended Answers

All 10 Replies

Clock() is not "that" accurate but it should be accurate enough, unless
you need more accuracy.

Try it out first and then decide.

#include<ctime>
#include<iostream>
using namespace std;

int main()
{
    long clk_strt = clock();

   // some code goes here to test

    long clk_end = clock();
   cout<<"Difference in milliseconds : "<<clk_end - clk_strt << endl;
  return 0;
}

Ok, i've tested clock(), and so far with out moving where the code is I have got values between 12 and 70+ milliseconds. Admitedly most of them are around 25 but still this isnt accurate enough for me really.

I tried clock() and so far without moving the placement of the code I have gotten values between 12 and 80 for the running time, this isn't accurate enough. Any other suggestions?

If you are running on windows then , this might help you

If you are running on windows then , this might help you

Looked at the support.microsoft link and there code looks nothing like C++ code, they also talk about entering their code into a module (???). I've done a C++ module in my second year and nothing of this sort has come up.

According to Ark M in this thread, if you want microseconds, you need something OS-specific.

http://www.daniweb.com/forums/thread134643.html

Ancient Dragon's idea in this thread is to do something a million times, time it in seconds using the standard C functions, then divide by a million. Depending on what you need, that may be feasible or not for you.

Some good threads came up when I googled "c++ time microseconds". Among them, "struct timeval" for Linux:

http://www.gnu.org/s/libc/manual/html_node/Elapsed-Time.html

And this, where a poster asked essentially what you are asking:

http://www.unix.com/high-level-programming/1991-time-microseconds.html

These are using Linux. For Windows, this came up:

http://www.decompile.com/cpp/faq/windows_timer_api.htm

A few examples came up. The one I found had some errors in it, so I played around. I had never used this before, but it seems to work (i.e. gives reasonable times). The most time consuming part for me was figuring out how to use LARGE_INTEGER.

Not sure what you mean by "modules". Plain old #including windows.h seems to do the job.

#include <windows.h>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
  LARGE_INTEGER numTicksPerSecond;
  LARGE_INTEGER startTime; 
  LARGE_INTEGER endTime; 

  // get the high resolution counter's accuracy
  QueryPerformanceFrequency(&numTicksPerSecond);

  // start
  QueryPerformanceCounter(&startTime);
  
  // do stuff
  
  for (int i = 0; i < 50000000; i++)
     rand ();

  // end
  QueryPerformanceCounter(&endTime);
  
  LONGLONG numTicks = endTime.QuadPart - startTime.QuadPart;
  double numSeconds = (((double) numTicks) / (double) numTicksPerSecond.QuadPart);

  cout << "Num Ticks Per Second : " << numTicksPerSecond.QuadPart << endl;
  cout << "Start " << startTime.QuadPart << endl;
  cout << "End : " << endTime.QuadPart << endl;
  cout << "Num Ticks : " << numTicks << endl;
  cout << "Num seconds : " << numSeconds << endl;
  
  cin.get (); 
  return 0;
}

I got 14,318,180 ticks per second, which is far faster than a microsecond.

I'm having the same kind of problem I had before with clock(), in that the time it takes to exectute the code flucuates alot. Why does this happen? I don't change the position of the code, any hardware settings.

I'm having the same kind of problem I had before with clock(), in that the time it takes to exectute the code flucuates alot. Why does this happen? I don't change the position of the code, any hardware settings.

Impossible to know without knowing what the code does and without knowing what else might be competing for your computer's resources while running the program, some of which might have a higher or lower scheduling priority. Try running it with as few other programs/processes running as possible. See if that removes some of the fluctuations.

Impossible to know without knowing what the code does and without knowing what else might be competing for your computer's resources while running the program, some of which might have a higher or lower scheduling priority. Try running it with as few other programs/processes running as possible. See if that removes some of the fluctuations.

I do run the minimal number of programs when doing this, I have even tried to reboot so I get only the command prompt but that option seems to have been removed in Windows 7.

My code is fairly simple, it's the 4th order runge kutta algorithm.

Right now my code is like this:

#include <windows.h>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main(){
    
    float x, y, k1, k2, k3, k4, h = 0.022222, a;
  

    LARGE_INTEGER numTicksPerSecond;
    
    LARGE_INTEGER startTime;
    
    LARGE_INTEGER endTime;   
  
  
  
	cout<<"This program uses the fourth order Runge-Kutta algorithm to solve an O.D.E in the for of dx/dt =ax"<<endl<<endl;

	cout<<"Please choose a value for a"<<endl;

	cin>>a;

	cout<<"Please choose a initial condition"<<endl;
	cin>>x;
	
	QueryPerformanceFrequency(&numTicksPerSecond);
    
    QueryPerformanceCounter(&startTime);
    
    long clock_start = clock();
    for(int i=0;i<46;i++){
		k1 = a*x;
		k2 = a*(x+h*k1/2);
		k3 = a*(x+h*k2/2);
		k4 = a*(x+h*k3);

		y = x + h/6*(k1 + 2*k2 + 2*k3 + k4);

		x = y;
		cout<<"Solution is x = "<<x<<endl;
	}
#
QueryPerformanceCounter(&endTime);
#
 
#
LONGLONG numTicks = endTime.QuadPart - startTime.QuadPart;
#
double numSeconds = (((double) numTicks) / (double) numTicksPerSecond.QuadPart);
#
 
#
cout << "Num Ticks Per Second : " << numTicksPerSecond.QuadPart << endl;
#
cout << "Start " << startTime.QuadPart << endl;
#
cout << "End : " << endTime.QuadPart << endl;
#
cout << "Num Ticks : " << numTicks << endl;
#
cout << "Num seconds : " << numSeconds << endl;
#
 
#
cin.get (); 
	
}

I do run the minimal number of programs when doing this, I have even tried to reboot so I get only the command prompt but that option seems to have been removed in Windows 7.

My code is fairly simple, it's the 4th order runge kutta algorithm.

It seems to fluctuate between 0.03 and 0.08 seconds for me. I don't know enough about scheduling algorithms to know what would cause that fluctuation, or if that would be a factor in programs that run for such a sort period of time. Maybe they run all the way from start to finish without being paused, maybe not. I really don't know. Good luck on this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.