-/** @file fcntimer.cpp
+/** @file timer.cpp
*
- * Function execution timer. */
+ * A simple stop watch class. */
/*
* GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include "times.h"
-#include <stdio.h>
-#include <sys/resource.h>
+timer::timer(void) : on(false)
+{
+ getrusage(RUSAGE_SELF, &used1);
+ getrusage(RUSAGE_SELF, &used2);
+}
+void timer::start(void)
+{
+ on = true;
+ getrusage(RUSAGE_SELF, &used1);
+ getrusage(RUSAGE_SELF, &used2);
+}
-// fcntimer() is a little wrapper around GiNaC's automated checks. All those
-// functions are passed void and return unsigned. fcntimer() accepts one such
-// function fcn(), returns its result and as a side-effect prints to stdout how
-// much CPU time was consumed by fcn's execution in the fashion "(0.07s)\n".
-unsigned fcntimer(unsigned fcn())
+void timer::stop(void)
{
- unsigned fcnresult;
- struct rusage used1, used2;
- double elapsed;
+ on = false;
+ getrusage(RUSAGE_SELF, &used2);
+}
- // time the execution of the function:
+void timer::reset(void)
+{
getrusage(RUSAGE_SELF, &used1);
- fcnresult = fcn();
getrusage(RUSAGE_SELF, &used2);
+}
- // add elapsed user and system time in microseconds:
+double timer::read(void)
+{
+ double elapsed;
+ if (this->running())
+ getrusage(RUSAGE_SELF, &used2);
elapsed = ((used2.ru_utime.tv_sec - used1.ru_utime.tv_sec) +
(used2.ru_stime.tv_sec - used1.ru_stime.tv_sec) +
(used2.ru_utime.tv_usec - used1.ru_utime.tv_usec) / 1e6 +
(used2.ru_stime.tv_usec - used1.ru_stime.tv_usec) / 1e6);
+ // round to 10ms for safety:
+ return 0.01*int(elapsed*100+0.5);
+}
- printf("(%.2fs)\n", elapsed);
-
- return fcnresult;
+bool timer::running(void)
+{
+ return on;
}