lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
|
00001 /*****************************************************/ 00002 /* lean Time (c) Tobias Zirr 2011 */ 00003 /*****************************************************/ 00004 00005 #ifndef LEAN_TIME_TIMER 00006 #define LEAN_TIME_TIMER 00007 00008 #include "../lean.h" 00009 #include <ctime> 00010 00011 namespace lean 00012 { 00013 namespace time 00014 { 00015 00017 class timer 00018 { 00019 private: 00020 time_t m_time; 00021 00022 public: 00024 LEAN_INLINE timer() 00025 : m_time(::time(nullptr)) { } 00026 00028 LEAN_INLINE void tick() { ::time(&m_time); } 00029 00031 LEAN_INLINE double seconds() const { return ::difftime(::time(nullptr), m_time); } 00033 LEAN_INLINE double milliseconds() const { return seconds() * 1000; } 00034 }; 00035 00036 // Old C standard definition 00037 #ifndef CLOCKS_PER_SEC 00038 #define CLOCKS_PER_SEC CLK_TCK 00039 #endif 00040 00042 class clocktimer 00043 { 00044 private: 00045 clock_t m_time; 00046 00047 public: 00049 LEAN_INLINE clocktimer() 00050 : m_time(::clock()) { } 00051 00053 LEAN_INLINE void tick() { m_time = ::clock(); } 00054 00056 LEAN_INLINE double seconds() const { return (double) (::clock() - m_time) * (1.0 / CLOCKS_PER_SEC); } 00058 LEAN_INLINE double milliseconds() const { return (double) (::clock() - m_time) * (1000.0 / CLOCKS_PER_SEC); } 00059 }; 00060 00061 } // namespace 00062 00063 using time::timer; 00064 using time::clocktimer; 00065 00066 } // namespace 00067 00068 #endif