2

I'm using boost::timer to time a section of my code. If I run the code with one thread:

$ time ./runfoo 1
Took 2.08s

real    0m2.086s
user    0m1.611s
sys     0m0.475s

2.08 is the output of t.elapsed().

Yet if I run the code with 4 threads:

$ time ./runfoo 4
Took 2.47s

real    0m1.022s
user    0m1.834s
sys     0m0.628s

It seems to be tracking user+sys time, not real time.

How do I make it track real time? I'm using Boost 1.46. To be more specific, the timer is in the main thread, which just calls a function that ends up using the multiple threads.

EDIT: The relevant source looks something like this:

boost::asio::io_service ios;
boost::thread_group pool; 
boost::asio::io_service::work work(ios);
pool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
pool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
pool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
pool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
{
    boost::timer t;

    function_which_posts_to_ios(ios);

    printf("Took %.2fs\n", t.elapsed();
}

As to what output I expect, I'd like the program in the 2nd run to print "Took 1.02s" instead of "Took 2.47s", as 1.02s was the actual amount of seconds that elapsed.

Claudiu
  • 224,032
  • 165
  • 485
  • 680

1 Answers1

2

It appears that you are using the deprecated Version 1 timers where the semantics of elapsed() was not consistent across platforms, wall-clock time on some and CPU time on others. In the Version 2 cpu_timer, the elapsed() method returns a struct which has distinct members for real, user, and system time.

If you cannot use the Version 2 API, you can use boost::posix_time instead to measure wall clock time. See c++ boost get current time in milliseconds.

Community
  • 1
  • 1
rhashimoto
  • 15,650
  • 2
  • 52
  • 80
  • Yea those don't exist for the version of boost I'm using. Looks like the question you linked has something I could use - thanks! – Claudiu Apr 26 '14 at 01:45