From owner-freebsd-questions Thu Jul 10 00:06:41 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id AAA11345 for questions-outgoing; Thu, 10 Jul 1997 00:06:41 -0700 (PDT) Received: from mailhub.iastate.edu (mailhub.iastate.edu [129.186.1.102]) by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id AAA11333 for ; Thu, 10 Jul 1997 00:06:36 -0700 (PDT) Received: from pseudo.cc.iastate.edu (pseudo.cc.iastate.edu [129.186.142.93]) by mailhub.iastate.edu (8.8.5/8.8.5) with SMTP id CAA18543; Thu, 10 Jul 1997 02:06:33 -0500 (CDT) Message-ID: <33C489A8.2781E494@iastate.edu> Date: Thu, 10 Jul 1997 02:05:12 -0500 From: Kent Vander Velden Organization: Iowa State University X-Mailer: Mozilla 3.01Gold (X11; I; FreeBSD 3.0-CURRENT i386) MIME-Version: 1.0 To: freebsd-questions@freebsd.org Subject: getrusage and small system time usages Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-questions@freebsd.org X-Loop: FreeBSD.org Precedence: bulk Often I use getrusage() to time a section of code. However, generally I get frustrated with the effecitveness of getrusage() since I often get negative numbers! Tonight, I got negative numbers again and thought perhaps someone could suggest a reason for this. Even if the interval that I am timing is really small, I see no reason the value would be negative. Zero would be more believable. The following contains code that I was using: class Clock { protected: rusage start; rusage end; public: Clock() { getrusage(RUSAGE_SELF, &start); } ~Clock() { getrusage(RUSAGE_SELF, &end); double st=(end.ru_stime.tv_sec - start.ru_stime.tv_sec +(end.ru_stime.tv_usec - start.ru_stime.tv_usec)/(double)10e6); double ut=(end.ru_utime.tv_sec - start.ru_utime.tv_sec +(end.ru_utime.tv_usec - start.ru_utime.tv_usec)/(double)10e6); cout << "System time requirement: " << st << " sec." << endl << "User time requirement: " << ut << " sec." << endl << "Total time requirement: " << st+ut << " sec." << endl; } }; Thanks.