C++ - “rusage” statistics

I'm trying to use “rusage” statistics in my program to get data similar to that of the time tool. However, I'm pretty sure that I'm doing something wrong. The values seem about right but can be a bit weird at times. I didn't find good resources online. Does somebody know how to do it better?

Sorry for the long code.

class StopWatch {
public:
    void start() {
        getrusage(RUSAGE_SELF, &m_begin);
        gettimeofday(&m_tmbegin, 0);
    }

    void stop() {
        getrusage(RUSAGE_SELF, &m_end);
        gettimeofday(&m_tmend, 0);
        timeval_sub(m_end.ru_utime, m_begin.ru_utime, m_diff.ru_utime);
        timeval_sub(m_end.ru_stime, m_begin.ru_stime, m_diff.ru_stime);
        timeval_sub(m_tmend, m_tmbegin, m_tmdiff);
    }

    void printf(std::ostream& out) const {
        using namespace std;

        timeval const& utime = m_diff.ru_utime;
        timeval const& stime = m_diff.ru_stime;

        format_time(out, utime);
        out << "u ";
        format_time(out, stime);
        out << "s ";
        format_time(out, m_tmdiff);
    }

private:
    rusage m_begin;
    rusage m_end;
    rusage m_diff;
    timeval m_tmbegin;
    timeval m_tmend;
    timeval m_tmdiff;

    static void timeval_add(timeval const& a, timeval const& b, timeval& ret) {
        ret.tv_usec = a.tv_usec + b.tv_usec;
        ret.tv_sec = a.tv_sec + b.tv_sec;
        if (ret.tv_usec > 999999) {
            ret.tv_usec -= 1000000;
            ++ret.tv_sec;
        }
    }

    static void timeval_sub(timeval const& a, timeval const& b, timeval& ret) {
        ret.tv_usec = a.tv_usec - b.tv_usec;
        ret.tv_sec = a.tv_sec - b.tv_sec;
        if (a.tv_usec < b.tv_usec) {
            ret.tv_usec += 1000000;
            --ret.tv_sec;
        }
    }

    static void format_time(std::ostream& out, timeval const& tv) {
        using namespace std;
        long usec = tv.tv_usec;
        while (usec >= 1000)
            usec /= 10;
        out << tv.tv_sec << '.' << setw(3) << setfill('0') << usec;
    }
}; // class StopWatch
This question and answers originated from www.stackoverflow.com
Question by (8/23/2008 12:57:16 PM)

Answer

What is the purpose of:

while (usec >= 1000)
    usec /= 10;

I gather that you want the most significant three digits of the usec; in that case, the most straightforward way I can think of is to divide usec by 1000, and be done with that.

Test cases:

  • 999999 ⇒ 999
  • 99999 ⇒ 999 (should be 099)
  • 9999 ⇒ 999 (should be 009)
  • 999 ⇒ 999 (should be 000)

Find More Answers
Related Topics  c++  unix  time  profiling
Related Questions
  • SQL Server: Index utilization statistics?

    Is there a way in SQL Server to get a report of index usage? i know starting with SQL Server 2005 , you can get reports of top resource-using queries , based on what's in the Plan Cache : …
  • Tool for program statistics

    Is there a tool which is able to parse my source code (fortran, C or C++) and return statistics such as the number of loops, the average loop size, the number of functions, the number of function ca…
  • cmake: compilation statistics

    I need to figure out which translation units need to be restructured to improve compile times, How do I get hold of the compilation time, using cmake, for my translation units ?
  • Generating statistics from Git repository

    I'm looking for some good tools/scripts that allow me to generate a few statistics from a git repository. I've seen this feature on some code hosting sites, and they contained information like... …
  • Computer usage statistics from clients

    I want to get workers' computer usage report for many computers. For example: daily messenger usage is 3 hours, photoshop usage 5 hours, etc. Is there any program which has a centralized reportin…
  • What statistics concepts are useful for profiling?

    I've been meaning to do a little bit of brushing up on my knowledge of statistics. One area where it seems like statistics would be helpful is in profiling code. I say this because it seems like pro…
  • Statistics on time estimates for web application

    I have been asked to help estimate the time it would take to develop a web application. I will not be involved in the actual programming, but I am participating as an "experienced" programmer. The a…
  • Best library for statistics in C++?

    I'm looking for high performance code (needs to run in real-time), preferably open source, but if there is nothing that's free and high-perf, I'll take something well supported and of high quality f…
  • Looking for design advise - Statistics reporter

    I need to implement a statistics reporter - an object that prints to screen bunch of statistic. This info is updated by 20 threads. The reporter must be a thread itself that wakes up every 1 sec,…
  • Intercepting traffic to memcached for statistics/analysis

    I want to setup a statistics monitoring platform to watch a specific service, but I'm not quiet sure how to go about it. Processing the intercepted data isn't my concern, just how to go about it. On…