sprint 1-alpha
sprint/timer.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::timer
00003 *
00004 *   Copyright (C) 2004-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00027 #ifndef _SPRINT_TIMER_H
00028 #define _SPRINT_TIMER_H
00029   
00030 #ifdef WIN32
00031  #include <windows.h>
00032 #else
00033  #include <sys/time.h>
00034  #include <time.h>
00035 #endif
00036 
00037 namespace sprint {
00038 
00040 class rtc {
00041 
00042 #ifdef WIN32
00043 
00044         double time_factor;    // Time Scaling Factor
00045 public:
00046         rtc() 
00047                 {
00048                 LONGLONG perf_cnt;
00049                 
00050                 if (QueryPerformanceFrequency((LARGE_INTEGER *) &perf_cnt)) 
00051                         {
00052                      time_factor=1.0/(double)perf_cnt; 
00053                         } 
00054                 }
00055 
00057         inline double getTime() const
00058                 {
00059                 LONGLONG time;
00060 
00061                 QueryPerformanceCounter((LARGE_INTEGER *) &time);
00062                 return (time * time_factor);
00063                 }
00064                 
00065 #else
00066 
00067         /*
00068         double time_factor;    // Time Scaling Factor
00069         inline unsigned long long rdtsc()       
00070         {
00071         unsigned long long v; 
00072 
00073          __asm__ ("rdtsc"  : "=A" (v)); 
00074 
00075         return v; 
00076         }
00077         */
00078 public:
00079         rtc() 
00080                 { 
00081                 /*
00082                 long long v0, v1;
00083                 v0 = rdtsc();
00084                 usleep(1000000);
00085                 v1 = rdtsc();
00086                 time_factor = 1.0 / (double) (v1 - v0);
00087                 */
00088                 }
00089  
00091         inline double getTime() const
00092                 {
00093                 /*
00094                 long long v = rdtsc();
00095                 return (v * time_factor);
00096                 */
00097                 struct timeval tv;
00098                 gettimeofday(&tv, NULL);
00099                 return (tv.tv_sec*1.0 + tv.tv_usec * 0.000001);
00100                 }
00101 #endif
00102 
00104         inline unsigned long getTimeMs() const
00105                 {
00106                 return (unsigned long)(getTime() * 1000.0);
00107                 }
00108 
00110         inline unsigned long getTimeSec() const
00111                 {
00112                 return (unsigned long)(getTime() );
00113                 }
00114 };
00115 
00124 class timer: public rtc {
00126         double user_time;
00127 public:
00128         timer()
00129         {
00130         user_time = rtc::getTime();
00131         }
00132 
00134         inline void start()
00135         {
00136         user_time = rtc::getTime();
00137         }
00138 
00140         inline double getTime() const
00141         {
00142         return rtc::getTime() - user_time;
00143         }
00144 
00145 };
00146 
00147 }
00148 
00149 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines