[chrono] using user + system process cpu time
Dear Chrono maintainers,
Here's what I'd like to do:
#include
Le 26/08/11 13:01, Julian Gonggrijp a écrit :
Dear Chrono maintainers,
Here's what I'd like to do:
#include
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?
Hi, I see a way a little bit verbose but not too much complicated: clock::duration delta = end-start; process_cpu_clock_times times=delta.count(); return (process_user_cpu_clock::duration(times.user) + process_system_cpu_clock::duration(times.system)); Hopping this works, Vicente
Vicente J. Botet Escriba wrote:
Le 26/08/11 13:01, Julian Gonggrijp a écrit :
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?
I see a way a little bit verbose but not too much complicated:
clock::duration delta = end-start; process_cpu_clock_times times=delta.count(); return (process_user_cpu_clock::duration(times.user) + process_system_cpu_clock::duration(times.system));
Thanks! It's a bit verbose indeed but I'm already glad there is a way that doesn't involve two clocks. -Julian
participants (2)
-
Julian Gonggrijp
-
Vicente J. Botet Escriba