How do you time execution time?. I am writing a program in C++ to calculate Pi with a desired degree of precision/digits after the decimal point, and I have to calculate the time it takes to calculate for each desired precision. Can anyone help me? :o

Recommended Answers

All 7 Replies

If you want any decent timings, you'll use a profiler. But you can use a poor man's timer with the standard library. For example:

#include <ctime>

std::clock_t start = std::clock();
/* Stuff to time here */
std::cout<< ( ( std::clock() - start ) / (double)CLOCKS_PER_SEC ) <<'\n';

But the resolution isn't very good for most purposes.

Thank you Narue.

clock_t is not a part of namespace std

Yes it is!

Plz try to compile this code.....i am using visual studio 6.0..where am i going wrong??

#include<iostream>
#include<ctime>

int main()
{

	std::clock_t start=std::clock();
	std::cout<<"Check This Out\n";
	std::clock_t end=std::clock();

return 0;
}

>where am i going wrong??
You're using Visual C++ 6.0. It's a notoriously awful compiler, even if you take into account that it doesn't really try very hard to comply with the draft standard. I used it for too long, and let me say that when you have to replace your compiler's standard library with your own implementation to get it to work right, that's a Bad Thing(TM).

p.s. Dev-C++ is free, and the latest Visual C++ is also free for the time being. But the retail Standard edition isn't that expensive. Get a better compiler or restrict yourself to C, and you'll be much happier.

>where am i going wrong??
You're using Visual C++ 6.0. It's a notoriously awful compiler, even if you take into account that it doesn't really try very hard to comply with the draft standard. I used it for too long, and let me say that when you have to replace your compiler's standard library with your own implementation to get it to work right, that's a Bad Thing(TM).

p.s. Dev-C++ is free, and the latest Visual C++ is also free for the time being. But the retail Standard edition isn't that expensive. Get a better compiler or restrict yourself to C, and you'll be much happier.

Heh. I just can't win...
First, MFC.
Then, trying to use an old piece o' Schlidt's as a reference.
Now, I find out the compiler's crummy.

Thank goodness for this forum. I'd probably be a lot further behind my personally-set goals if it weren't for Daniweb.
Too late to change, but I'm sure I'll be doing more coding in the future... on Dev-C++.

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.