Boost logo

Boost Users :

Subject: [Boost-users] [chrono] using user + system process cpu time
From: Julian Gonggrijp (j.gonggrijp_at_[hidden])
Date: 2011-08-26 07:01:15


Dear Chrono maintainers,

Here's what I'd like to do:

#include <boost/chrono.hpp>
namespace bcr = boost::chrono;
typedef bcr::process_cpu_clock clock;

bcr::milliseconds foo ( ) {
    clock::time_point start = clock::now();
    // run a test
    clock::time_point end = clock::now();
    return (end.user() - start.user()) + (end.sys() - start.sys());
}

However, it seems (from the manual) that bcr::process_cpu_clock
doesn't provide an interface for retrieving the individual user and
system components (except by passing it through a std::stringstream,
but that's cumbersome). The only alternative that I can think of is to
use bcr::process_user_cpu_clock and bcr::process_system_cpu_clock
separately, like this:

typedef bcr::process_user_cpu_clock uclock;
typedef bcr::process_system_cpu_clock sclock;

bcr::milliseconds foo ( ) {
    uclock::time_point ustart = uclock::now();
    sclock::time_point sstart = sclock::now();
    // run a test
    uclock::time_point uend = uclock::now();
    sclock::time_point send = sclock::now();
    return (uend - ustart) + (send - sstart);
}

But the latter approach is less elegant and I'm afraid that the result
might be less reliable.

Could you please advice me on the best way to find both the user and
system components of the process cpu time taken by a piece of code?

Thanks in advance,
-Julian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net