|
-
July 27th, 2010, 03:37 AM
#1
Timing my function
How to time my function in linux ?
-
July 27th, 2010, 04:05 AM
#2
-
July 27th, 2010, 08:40 AM
#3
Re: Timing my function
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 27th, 2010, 09:29 AM
#4
Re: Timing my function
You should really profile your code. However, if you wish to do a crude timing, and you want something that is cross-platform, then you could use boost posix microsecond clock. Here is an example header:
Code:
//timer.hpp
#include <boost/date_time/posix_time/posix_time.hpp>
namespace timing
{
struct timer
{
timer()
:start_time_()
{
reset();
}
void reset()
{
start_time_ = boost::posix_time::ptime(boost::posix_time::microsec_clock::local_time());
}
double elapsed() const
{
double result(0);
boost::posix_time::ptime now(boost::posix_time::microsec_clock::local_time());
result = (now-start_time_).total_milliseconds()/1000.0;
return result;
}
private:
boost::posix_time::ptime start_time_;
};
}
And here is a test application to run it with:
Code:
//main.cpp
#include <boost/thread/thread.hpp>
#include "timer.hpp"
int main()
{
timing::timer timer;
//I've put a pause in here, but you should swap it with your
//function call instead.
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
std::cout << timer.elapsed() << std::endl;
std::cout << "Press return to continue" << std::endl;
std::cin.get();
}
It's never going to be as good as using a proper profiler, but I hope it helps anyway.
By the way, assuming you have installed boost (if its not installed try installing it via the package manager), to compile the above code under linux, you'll need to type something like:
Code:
g++ main.cpp -O2 -lboost_thread -lboost_system
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|