X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=check%2Ftimer.cpp;h=d375313f82df49e91eb1e14bdbe7e816c9f4cadc;hp=0b978a7ded82aadb3cea7a1ed4d68eda84c45d8e;hb=edf1ae46a926d0a718063c149b78c1b9a7ec2043;hpb=6afdf0650491483f38a9e8dc4edea9243175cdf2 diff --git a/check/timer.cpp b/check/timer.cpp index 0b978a7d..d375313f 100644 --- a/check/timer.cpp +++ b/check/timer.cpp @@ -3,7 +3,7 @@ * A simple stop watch class. */ /* - * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,51 +20,81 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#ifdef HAVE_RUSAGE #include #include +#include +#else +#include +#endif +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "timer.h" timer::timer() : on(false) { +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used1); used2.ru_utime = used1.ru_utime; used2.ru_stime = used1.ru_stime; +#else + used1 = clock(); + used2 = used1; +#endif } void timer::start() { on = true; +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used1); used2.ru_utime = used1.ru_utime; used2.ru_stime = used1.ru_stime; +#else + used1 = clock(); + used2 = used1; +#endif } void timer::stop() { on = false; +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used2); +#else + used2 = clock(); +#endif } void timer::reset() { +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used1); used2.ru_utime = used1.ru_utime; used2.ru_stime = used1.ru_stime; +#else + used1 = clock(); + used2 = used1; +#endif } double timer::read() { double elapsed; +#ifdef HAVE_RUSAGE if (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) * 1e-6 + - (used2.ru_stime.tv_usec - used1.ru_stime.tv_usec) * 1e-6); - // Results more accurate than 10ms are pointless: - return 0.01*int(elapsed*100+0.5); + return ((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) * 1e-6 + + (used2.ru_stime.tv_usec - used1.ru_stime.tv_usec) * 1e-6); +#else + if (running()) + used2 = clock(); + return double(used2 - used1)/CLOCKS_PER_SEC; +#endif } bool timer::running()