X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=check%2Ftimer.cpp;h=02b925548a7d4a0e726460dac853681da6aea4bc;hp=492339b634741cd00fde23b3d7485b5b34a9dd1c;hb=01d804753f9f140b2e470e10ff2b97ddae1627dd;hpb=383d5eb3b0f0506810d9105a268f939125bfc347;ds=sidebyside diff --git a/check/timer.cpp b/check/timer.cpp index 492339b6..02b92554 100644 --- a/check/timer.cpp +++ b/check/timer.cpp @@ -3,7 +3,7 @@ * A simple stop watch class. */ /* - * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2011 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 @@ -17,50 +17,87 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "times.h" +#ifdef HAVE_RUSAGE +#include +#include +#include +#else +#include +#endif -timer::timer(void) : on(false) +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include "timer.h" + +timer::timer() : on(false) { +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used1); - getrusage(RUSAGE_SELF, &used2); + used2.ru_utime = used1.ru_utime; + used2.ru_stime = used1.ru_stime; +#else + used1 = clock(); + used2 = used1; +#endif } -void timer::start(void) +void timer::start() { on = true; +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used1); - getrusage(RUSAGE_SELF, &used2); + used2.ru_utime = used1.ru_utime; + used2.ru_stime = used1.ru_stime; +#else + used1 = clock(); + used2 = used1; +#endif } -void timer::stop(void) +void timer::stop() { on = false; +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used2); +#else + used2 = clock(); +#endif } -void timer::reset(void) +void timer::reset() { +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used1); - getrusage(RUSAGE_SELF, &used2); + used2.ru_utime = used1.ru_utime; + used2.ru_stime = used1.ru_stime; +#else + used1 = clock(); + used2 = used1; +#endif } -double timer::read(void) +double timer::read() { double elapsed; - if (this->running()) +#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) / 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); + 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(void) +bool timer::running() { return on; }