Boost logo

Boost Users :

Subject: Re: [Boost-users] [chrono] using user + system process cpu time
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2011-08-26 13:15:58


Le 26/08/11 13:01, Julian Gonggrijp a écrit :
> 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?
>
>
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


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