timer.h
Go to the documentation of this file.
00001 /* This file is part of the Pangolin Project.
00002  * http://github.com/stevenlovegrove/Pangolin
00003  *
00004  * Copyright (c) 2011 Steven Lovegrove
00005  *
00006  * Permission is hereby granted, free of charge, to any person
00007  * obtaining a copy of this software and associated documentation
00008  * files (the "Software"), to deal in the Software without
00009  * restriction, including without limitation the rights to use,
00010  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the
00012  * Software is furnished to do so, subject to the following
00013  * conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be
00016  * included in all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00019  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00020  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00021  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00022  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00023  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00025  * OTHER DEALINGS IN THE SOFTWARE.
00026  */
00027 
00028 #ifndef PANGOLIN_TIMER_H
00029 #define PANGOLIN_TIMER_H
00030 
00031 #include "pangolin.h"
00032 
00033 #ifdef _UNIX_
00034 #include <sys/time.h>
00035 #endif
00036 
00037 #ifdef _WIN_
00038 #include <windows.h>
00039 #endif
00040 
00041 namespace pangolin
00042 {
00043 
00044 #ifdef _UNIX_
00045 typedef timeval basetime;
00046 
00047 inline basetime TimeNow()
00048 {
00049     basetime t;
00050     gettimeofday(&t,NULL);
00051     return t;
00052 }
00053 
00054 inline basetime TimeFromSeconds(double seconds)
00055 {
00056     basetime t;
00057     t.tv_sec = (time_t)seconds;
00058     t.tv_usec = (useconds_t)((seconds - t.tv_sec) * 1E6);
00059     return t;
00060 }
00061 
00062 inline basetime TimeAdd(basetime t1, basetime t2)
00063 {
00064     basetime t;
00065     t.tv_sec = t1.tv_sec + t2.tv_sec;
00066     t.tv_usec = t1.tv_usec + t2.tv_usec;
00067     if(t.tv_usec >= 1E6 )
00068     {
00069         t.tv_usec -= 1E6;
00070         t.tv_sec += 1;
00071     }
00072 
00073     return t;
00074 }
00075 
00076 inline double TimeDiff_s(basetime start, basetime end)
00077 {
00078     return (double)(end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) * 1E-6;
00079 }
00080 #endif
00081 
00082 #ifdef _WIN_
00083 typedef LARGE_INTEGER basetime;
00084 
00085 inline basetime TimeNow()
00086 {
00087     basetime t;
00088     QueryPerformanceCounter(&t);
00089     return t;
00090 }
00091 
00092 inline double TimeDiff_s(basetime start, basetime end)
00093 {
00094     LARGE_INTEGER f;
00095     QueryPerformanceFrequency(&f);
00096     return (end.QuadPart - start.QuadPart) / f.QuadPart;
00097 }
00098 
00099 inline basetime TimeFromSeconds(double seconds)
00100 {
00101     LARGE_INTEGER f;
00102     QueryPerformanceFrequency(&f);
00103     basetime t;
00104     t.QuadPart = (seconds * f);
00105     return t;
00106 }
00107 
00108 inline basetime TimeAdd(basetime t1, basetime t2)
00109 {
00110     basetime t;
00111     t.QuadPart t1.QuadPart + t2.QuadPart;
00112     return t;
00113 }
00114 #endif
00115 
00116 inline basetime WaitUntil(basetime t)
00117 {
00118     // TODO: use smarter sleep!
00119     basetime currtime = TimeNow();
00120     while( TimeDiff_s(currtime,t) > 0 )
00121         currtime = TimeNow();
00122     return currtime;
00123 }
00124 
00125 struct Timer
00126 {
00127     Timer()
00128     {
00129         Reset();
00130     }
00131 
00132     void Reset()
00133     {
00134         start = TimeNow();
00135     }
00136 
00137     double Elapsed_s()
00138     {
00139         basetime currtime = TimeNow();
00140         return TimeDiff_s(start,currtime);
00141     }
00142 
00143     basetime start;
00144 };
00145 
00146 }
00147 
00148 #endif //PANGOLIN_TIMER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


pangolin_wrapper
Author(s): Todor Stoyanov
autogenerated on Wed Feb 13 2013 14:03:25