Last active
April 27, 2020 03:36
-
-
Save murphypei/feafd5bb4ff59f3a68cc1617c2c45811 to your computer and use it in GitHub Desktop.
C++ auto time
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 characters
| #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_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment