Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/debug/timer.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2013 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 // WARNING: Do not install this header, because it is platform-specific.
00020 
00021 #ifndef MARSYAS_DEBUG_TIMER_INCLUDED
00022 #define MARSYAS_DEBUG_TIMER_INCLUDED
00023 
00024 #include <marsyas/common_source.h>
00025 #include <marsyas/export.h>
00026 
00027 #if defined(MARSYAS_LINUX)
00028 
00029 #include <time.h>
00030 
00031 namespace Marsyas { namespace Debug {
00032 
00033 class Timer
00034 {
00035   timespec m_start_cpu_time;
00036   timespec m_start_real_time;
00037   timespec m_cpu_time;
00038   timespec m_real_time;
00039 
00040 public:
00041   Timer()
00042   {}
00043 
00044   void start()
00045   {
00046     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &m_start_cpu_time);
00047     clock_gettime(CLOCK_REALTIME, &m_start_real_time);
00048   }
00049 
00050   void measure()
00051   {
00052     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &m_cpu_time);
00053     clock_gettime(CLOCK_REALTIME, &m_real_time);
00054   }
00055 
00056   double cpuTime()
00057   {
00058     return difference(m_cpu_time, m_start_cpu_time);
00059   }
00060 
00061   double realTime()
00062   {
00063     return difference(m_real_time, m_start_real_time);
00064   }
00065 
00066 private:
00067   double difference( const timespec &t, const timespec &t0 )
00068   {
00069     double seconds = t.tv_sec - t0.tv_sec;
00070     long nanoseconds = t.tv_nsec - t0.tv_nsec;
00071     seconds += (double) nanoseconds * 10.0e-10;
00072     return seconds;
00073   }
00074 };
00075 
00076 }} // namespace Marsyas::Debug
00077 
00078 #else
00079 
00080 #include <chrono>
00081 
00082 namespace Marsyas { namespace Debug {
00083 
00084 using namespace std::chrono;
00085 
00086 class marsyas_EXPORT Timer
00087 {
00088     high_resolution_clock::time_point m_real_time_start;
00089     high_resolution_clock::time_point m_real_time_measure;
00090 public:
00091   void start()
00092   {
00093       m_real_time_start = high_resolution_clock::now();
00094   }
00095   void measure()
00096   {
00097       m_real_time_measure = high_resolution_clock::now();
00098   }
00099   double cpuTime()
00100   {
00101       return 0.0;
00102   }
00103   double realTime()
00104   {
00105       duration<double> m_real_time_dur =
00106               m_real_time_measure - m_real_time_start;
00107       return m_real_time_dur.count();
00108   }
00109 };
00110 
00111 }} // namespace Marsyas::Debug
00112 
00113 #endif
00114 
00115 #endif // MARSYAS_DEBUG_TIMER_INCLUDED