How to time my function in linux ?
Printable View
How to time my function in linux ?
Google is your friend :)
http://stackoverflow.com/questions/5...-work-properly
(I expect the code in the second answer there does pretty much what you want)
http://stackoverflow.com/questions/5...th-c-and-linux
CodeGuru is your friend too. ;)
http://www.codeguru.com/forum/showthread.php?t=291294
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:
And here is a test application to run it with: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_;
};
}
It's never going to be as good as using a proper profiler, but I hope it helps anyway.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();
}
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