Last active
April 27, 2020 03:36
-
-
Save murphypei/feafd5bb4ff59f3a68cc1617c2c45811 to your computer and use it in GitHub Desktop.
Revisions
-
murphypei revised this gist
Apr 27, 2020 . 2 changed files with 11 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #include "auto_time.h" #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -50,14 +50,14 @@ uint64_t Timer::durationInUs() return lastTime - mLastResetTime; } AutoTimer::AutoTime(const char *file, const char *func, int line) : Timer() { mFile = ::strdup(file); mFunc = ::strdup(func); mLine = line; } AutoTimer::~AutoTime() { auto timeInUs = durationInUs(); printf("%s::%s::%d, cost time: %f ms\n", mFile, mFunc, mLine, (float)timeInUs / 1000.0f); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,15 +21,15 @@ class Timer uint64_t mLastResetTime; // microseconds }; class AutoTime : Time { public: AutoTime(const char *file, const char *func, int line); ~AutoTime(); AutoTime(const AutoTimer &) = delete; AutoTime(const AutoTimer &&) = delete; AutoTime &operator=(const AutoTimer &) = delete; AutoTime &operator=(const AutoTimer &&) = delete; private: int mLine; @@ -40,7 +40,7 @@ class AutoTimer : Timer } // namespace timer #ifdef OPEN_TIME_TRACE #define AUTOTIME timer::AutoTime ___t(__FILE__, __FUNCTION__, __LINE__) #else #define AUTOTIMER #endif -
murphypei revised this gist
Apr 26, 2020 . 3 changed files with 114 additions and 111 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,111 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ #include "timer.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #if defined(WINDOWS) #include <Windows.h> #else #include <sys/time.h> #endif namespace timer { Timer::Timer() { reset(); } Timer::~Timer() {} void Timer::reset() { #if defined(WINDOWS) LARGE_INTEGER time, freq; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&time); uint64_t sec = time.QuadPart / freq.QuadPart; uint64_t usec = (time.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart; mLastResetTime = sec * 1000000 + usec; #else struct timeval Current; gettimeofday(&Current, nullptr); mLastResetTime = Current.tv_sec * 1000000 + Current.tv_usec; #endif } uint64_t Timer::durationInUs() { #if defined(_MSC_VER) LARGE_INTEGER time, freq; QueryPerformanceCounter(&time); QueryPerformanceFrequency(&freq); uint64_t sec = time.QuadPart / freq.QuadPart; uint64_t usec = (time.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart; auto lastTime = sec * 1000000 + usec; #else struct timeval Current; gettimeofday(&Current, nullptr); auto lastTime = Current.tv_sec * 1000000 + Current.tv_usec; #endif return lastTime - mLastResetTime; } AutoTimer::AutoTimer(const char *file, const char *func, int line) : Timer() { mFile = ::strdup(file); mFunc = ::strdup(func); mLine = line; } AutoTimer::~AutoTimer() { auto timeInUs = durationInUs(); printf("%s::%s::%d, cost time: %f ms\n", mFile, mFunc, mLine, (float)timeInUs / 1000.0f); free(mFile); free(mFunc); } } // namespace timer This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ #include <stdint.h> namespace timer { class Timer { public: Timer(); ~Timer(); Timer(const Timer &) = delete; Timer(const Timer &&) = delete; Timer &operator=(const Timer &) = delete; Timer &operator=(const Timer &&) = delete; // reset timer void reset(); // get duration (us) from init or latest reset. uint64_t durationInUs(); protected: uint64_t mLastResetTime; // microseconds }; class AutoTimer : Timer { public: AutoTimer(const char *file, const char *func, int line); ~AutoTimer(); AutoTimer(const AutoTimer &) = delete; AutoTimer(const AutoTimer &&) = delete; AutoTimer &operator=(const AutoTimer &) = delete; AutoTimer &operator=(const AutoTimer &&) = delete; private: int mLine; char *mFunc; char *mFile; }; } // namespace timer #ifdef OPEN_TIME_TRACE #define AUTOTIMER timer::AutoTimer ___t(__FILE__, __FUNCTION__, __LINE__) #else #define AUTOTIMER #endif -
murphypei created this gist
Apr 16, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,111 @@ #ifndef AUTO_TIMER_H_ #define AUTO_TIMER_H_ #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #if defined(_MSC_VER) #include <Windows.h> #else #include <sys/time.h> #endif #if defined(_MSC_VER) // windows #if defined(BUILDING_DLL) #define NN_PUBLIC __declspec(dllexport) #elif defined(USING_DLL) #define NN_PUBLIC __declspec(dllimport) #else #define NN_PUBLIC #endif #else // unix #define NN_PUBLIC __attribute__((visibility("default"))) #endif namespace NN { struct NN_PUBLIC Timer { public: Timer() { reset(); } ~Timer() = default; Timer(const Timer &) = delete; Timer(const Timer &&) = delete; Timer &operator=(const Timer &) = delete; Timer &operator=(const Timer &&) = delete; // reset timer void reset() { #if defined(_MSC_VER) LARGE_INTEGER time, freq; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&time); uint64_t sec = time.QuadPart / freq.QuadPart; uint64_t usec = (time.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart; mLastResetTime = sec * 1000000 + usec; #else struct timeval Current; gettimeofday(&Current, nullptr); mLastResetTime = Current.tv_sec * 1000000 + Current.tv_usec; #endif } // get duration (us) from init or latest reset. uint64_t durationInUs() { #if defined(_MSC_VER) LARGE_INTEGER time, freq; QueryPerformanceCounter(&time); QueryPerformanceFrequency(&freq); uint64_t sec = time.QuadPart / freq.QuadPart; uint64_t usec = (time.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart; auto lastTime = sec * 1000000 + usec; #else struct timeval Current; gettimeofday(&Current, nullptr); auto lastTime = Current.tv_sec * 1000000 + Current.tv_usec; #endif return lastTime - mLastResetTime; } protected: uint64_t mLastResetTime; }; // AutoTime implementation with RAII class NN_PUBLIC AutoTime : Timer { public: AutoTime(int line, const char *func) : Timer() { mName = ::strdup(func); mLine = line; } ~AutoTime() { auto timeInUs = durationInUs(); printf("%s, %d, cost time: %f ms\n", mName, mLine, (float)timeInUs / 1000.0f); free(mName); } AutoTime(const AutoTime &) = delete; AutoTime(const AutoTime &&) = delete; AutoTime &operator=(const AutoTime &) = delete; AutoTime &operator=(const AutoTime &&) = delete; private: int mLine; char *mName; uint64_t mCurrentTime; }; } // namespace NN #ifdef OPEN_TIME_TRACE #define AUTOTIME NN::AutoTime ___t(__LINE__, __func__) #else #define AUTOTIME #endif #endif // AUTO_TIMER_H_