• Main Page
  • Packages
  • Classes
  • Files
  • File List
  • File Members

unlock/bci/acquire-c++/ITimer.hpp

Go to the documentation of this file.
00001 
00002 // Copyright (c) James Percent and Unlock contributors.
00003 // All rights reserved.
00004 // Redistribution and use in source and binary forms, with or without modification,
00005 // are permitted provided that the following conditions are met:
00006 //
00007 //    1. Redistributions of source code must retain the above copyright notice,
00008 //       this list of conditions and the following disclaimer.
00009 //    
00010 //    2. Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //
00014 //    3. Neither the name of Unlock nor the names of its contributors may be used
00015 //       to endorse or promote products derived from this software without
00016 //       specific prior written permission.
00017 
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00022 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00025 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 
00029 #ifndef ITIMER_HPP
00030 #define ITIMER_HPP
00031 
00032 #include <stdint.h>
00033 #include "Portability.hpp"
00034 
00035 class DllExport ITimer {
00036 public:
00037     virtual ~ITimer() {}
00038     virtual void start()=0;
00039     virtual uint32_t elapsedCycles()=0;
00040     virtual uint32_t elapsedMilliSecs()=0;
00041     virtual uint32_t elapsedMicroSecs()=0;    
00042     virtual int64_t getFrequency()=0;
00043     virtual int64_t getStartValue()=0;
00044 };
00045 
00046 #endif
00047 
00048 #if 0
00049 
00050 
00051 #include <iostream>
00052 #include <Windows.h>
00053 
00054 using namespace std;
00055 
00056 LARGE_INTEGER timerFreq_;
00057 LARGE_INTEGER counterAtStart_;
00058 
00059 void startTime()
00060 {
00061   QueryPerformanceFrequency(&timerFreq_);
00062   QueryPerformanceCounter(&counterAtStart_);
00063   cout<<"timerFreq_ = "<<timerFreq_.QuadPart<<endl;
00064   cout<<"counterAtStart_ = "<<counterAtStart_.QuadPart<<endl;
00065   TIMECAPS ptc;
00066   UINT cbtc = 8;
00067   MMRESULT result = timeGetDevCaps(&ptc, cbtc);
00068   if (result == TIMERR_NOERROR)
00069   {
00070     cout<<"Minimum resolution = "<<ptc.wPeriodMin<<endl;
00071     cout<<"Maximum resolution = "<<ptc.wPeriodMax<<endl;
00072   }
00073   else
00074   {
00075     cout<<"result = TIMER ERROR"<<endl;
00076   }
00077 }
00078 
00079 unsigned int calculateElapsedTime()
00080 {
00081   if (timerFreq_.QuadPart == 0)
00082   {
00083     return -1;
00084   }
00085   else
00086   {
00087     LARGE_INTEGER c;
00088     QueryPerformanceCounter(&c);
00089     return static_cast<unsigned int>( (c.QuadPart - counterAtStart_.QuadPart) * 1000 / timerFreq_.QuadPart );
00090   }
00091 }
00092 
00093 int main()
00094 {
00095   //Increasing the accuracy of Sleep to 1ms using timeBeginPeriod
00096   timeBeginPeriod(1); //Add Winmm.lib in Project
00097   unsigned int diffTime = 0, lastTime = 0, newTime = 0;
00098   startTime();
00099   lastTime = calculateElapsedTime();
00100   cout<<"Start Time = "<<lastTime<<endl;
00101 
00102   Sleep(100);
00103   newTime = calculateElapsedTime();
00104   diffTime = newTime - lastTime;
00105   cout<<"Time after 100ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
00106   lastTime = newTime;
00107 
00108   Sleep(100);
00109   newTime = calculateElapsedTime();
00110   diffTime = newTime - lastTime;
00111   cout<<"Time after 100ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
00112   lastTime = newTime;
00113 
00114   Sleep(5);
00115   newTime = calculateElapsedTime();
00116   diffTime = newTime - lastTime;
00117   cout<<"Time after   5ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
00118   lastTime = newTime;
00119 
00120   Sleep(50);
00121   newTime = calculateElapsedTime();
00122   diffTime = newTime - lastTime;
00123   cout<<"Time after  50ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
00124 
00125   timeEndPeriod(1); //Must be called if timeBeginPeriod() was called
00126   return 0;
00127 }
00128 #endif

Generated on Mon Nov 10 2014 11:09:07 for The Unlock Project by  doxygen 1.7.1