Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Timer.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Thu Nov  8 12:36:38 EST 2001 by ken@xorian.net
00020 //      modified on Fri Jul 19 09:50:03 PDT 1996 by heydon
00021 
00022 #include <sys/time.h>
00023 #include <Basics.H>
00024 #include "Timer.H"
00025 
00026 void Timer::T::Start() throw ()
00027 {
00028     assert(!this->running);
00029     this->running = true;
00030     struct timezone tzp;
00031     int code = gettimeofday(&(this->startTime), &tzp); assert(code == 0);
00032 }
00033 
00034 Timer::MicroSecs Timer::T::Stop() throw ()
00035 {
00036     assert(this->running);
00037     this->running = false;
00038     struct timeval endTime;
00039     struct timezone tzp;
00040     int code = gettimeofday(&endTime, &tzp); assert(code == 0);
00041     MicroSecs delta = 1000000UL * (endTime.tv_sec - this->startTime.tv_sec);
00042     delta += (endTime.tv_usec - this->startTime.tv_usec);
00043     this->elapsed += delta;
00044     return delta;
00045 }
00046 
00047 Timer::MicroSecs Timer::T::Reset() throw ()
00048 {
00049     assert(!this->running);
00050     MicroSecs res = this->elapsed;
00051     this->elapsed = 0UL;
00052     return res;
00053 }
00054 
00055 Timer::MicroSecs Timer::Grain;
00056 
00057 static void Timer_InitGrain() throw ()
00058 /* The value for "Grain" is determined experimentally. */
00059 {
00060     const int Incr = 100;
00061     int cnt = 0;
00062     Timer::T timer;
00063     while (1) {
00064         timer.Start();
00065         for (int j = 0; j < cnt; j++) {
00066             int k = j/3; k *= 3;
00067         }
00068         Timer::MicroSecs t = timer.Stop();
00069         if (t != 0UL) {
00070             Timer::Grain = t;
00071             break;
00072         }
00073         cnt += Incr;
00074     }
00075 }
00076 
00077 class TimerInit {
00078   public:
00079     TimerInit() throw () { Timer_InitGrain(); }
00080 };
00081 static TimerInit t; // initialize granularity at start-up

Generated on Mon May 8 00:48:33 2006 for Vesta by  doxygen 1.4.2