0

I'm trying to measure the CPU time for functions within my C++ program. I am doing this using the boost library. When I run my program however I get a result of 0 seconds. I'm new to boost so if someone could point me in the right direction?

How can I make this return a time that I can actually use, such as 0.00156 or something?

Code: Inside main()

boost::timer::cpu_timer timer;

std::cout << tree1.search("Cork") << std::endl;

//Print CPU TIME
boost::timer::cpu_times elapsed = timer.elapsed();
std::cout << " CPU TIME: " << (elapsed.user + elapsed.system) / 1e9 << " seconds" << std::endl;
luke_k
  • 15
  • 3

1 Answers1

1

You can use boost chrono;

boost::timer::cpu_timer timer;

std::cout << tree1.search("Cork") << std::endl;

//Print CPU TIME
boost::chrono::duration<double> elapsed = boost::chrono::nanoseconds(timer.elapsed().user);
std::cout << " CPU TIME: " << seconds.count() << "s\n" << " seconds" << std::endl;
Rama
  • 3,222
  • 2
  • 11
  • 26
  • That still makes no difference. Could it be that the function just doesn't take long enough to execute and the CPU time is so small that it can't be displayed? – luke_k Mar 20 '17 at 21:18
  • While it's a good idea to use `chrono` instead of Boost. I doubt it has better time resolution. I wouldn't expect this to make any difference. – Fred Larson Mar 20 '17 at 21:28