<p>I'm trying to use “rusage” statistics in my program to get data similar to that of the <a href="http://en.wikipedia.org/wiki/Time_%28Unix%29" rel="nofollow noreferrer">time</a> 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?</p>

<p>Sorry for the long code.</p>

<pre><code>class StopWatch {
public:
    void start() {
        getrusage(RUSAGE_SELF, &amp;m_begin);
        gettimeofday(&amp;m_tmbegin, 0);
    }

    void stop() {
        getrusage(RUSAGE_SELF, &amp;m_end);
        gettimeofday(&amp;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&amp; out) const {
        using namespace std;

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

        format_time(out, utime);
        out &lt;&lt; "u ";
        format_time(out, stime);
        out &lt;&lt; "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&amp; a, timeval const&amp; b, timeval&amp; ret) {
        ret.tv_usec = a.tv_usec + b.tv_usec;
        ret.tv_sec = a.tv_sec + b.tv_sec;
        if (ret.tv_usec &gt; 999999) {
            ret.tv_usec -= 1000000;
            ++ret.tv_sec;
        }
    }

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

    static void format_time(std::ostream&amp; out, timeval const&amp; tv) {
        using namespace std;
        long usec = tv.tv_usec;
        while (usec &gt;= 1000)
            usec /= 10;
        out &lt;&lt; tv.tv_sec &lt;&lt; '.' &lt;&lt; setw(3) &lt;&lt; setfill('0') &lt;&lt; usec;
    }
}; // class StopWatch
</code></pre>

[{'answer_id': 26827, 'author': 'Mike Haboustak', 'author_id': 2146, 'author_profile': 'https://Stackoverflow.com/users/2146', 'pm_score': 2, 'selected': False, 'text': "<p>I think there's probably a bug somewhere in your composition of sec and usec. I can't really say what exactly without knowing the kinds of errors you're seeing. A rough guess would be that usec can never be > 999999, so you're relying on overflow to know when to adjust sec. It could also just be a problem with your duration output format.</p>\n\n<p>Anyway. Why not store the utime and stime components as float seconds rather than trying to build your own rusage on output? I'm pretty sure the following will give you proper seconds.  </p>\n\n<pre><code>static int timeval_diff_ms(timeval const&amp; end, timeval const&amp; start) {\n    int micro_seconds = (end.tv_sec  - start.tv_sec) * 1000000 \n        + end.tv_usec - start.tv_usec;\n\n    return micro_seconds;\n}\n\nstatic float timeval_diff(timeval const&amp; end, timeval const&amp; start) {\n    return (timeval_diff_ms(end, start)/1000000.0f);\n}\n</code></pre>\n\n<p>If you want to decompose this back into an rusage, you can always int-div and modulo.</p>\n"}, {'answer_id': 71401, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 3, 'selected': True, 'text': '<p>What is the purpose of:</p>\n\n<pre><code>while (usec &gt;= 1000)\n    usec /= 10;\n</code></pre>\n\n<p>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.</p>\n\n<p>Test cases:</p>\n\n<ul>\n<li>999999 ⇒ 999</li>\n<li>99999 ⇒ 999 (should be 099)</li>\n<li>9999 ⇒ 999 (should be 009)</li>\n<li>999 ⇒ 999 (should be 000)</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/24207', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1968/']