From db706769333a4cd5f4a9d5176dcf790256aefc23 Mon Sep 17 00:00:00 2001 From: Rong Mantle Bao Date: Sat, 9 Jul 2022 07:20:44 +0800 Subject: [PATCH 1/2] Use cross-platform std::chrono in getTickCount() Add conditional compilation directives to enable uses of std::chrono on supported compilers. Use std::chrono::steady_clock as a source to retrieve current tick count and clock frequency. Fixes opencv/opencv#6902. --- modules/core/src/system.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index adb957908d..dda62fb89e 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -302,6 +302,16 @@ DECLARE_CV_CPUID_X86 #endif +#if ((__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1800)) + #define HAVE_CXX11 1 +#else + #define HAVE_CXX11 0 +#endif + +#if HAVE_CXX11 + #include +#endif + namespace cv { @@ -840,7 +850,10 @@ bool useOptimized(void) int64 getTickCount(void) { -#if defined _WIN32 || defined WINCE +#if HAVE_CXX11 + std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); + return (int64)now.time_since_epoch().count(); +#elif defined _WIN32 || defined WINCE LARGE_INTEGER counter; QueryPerformanceCounter( &counter ); return (int64)counter.QuadPart; @@ -859,7 +872,11 @@ int64 getTickCount(void) double getTickFrequency(void) { -#if defined _WIN32 || defined WINCE +#if HAVE_CXX11 + using clock_period_t = std::chrono::steady_clock::duration::period; + double clock_freq = clock_period_t::den / clock_period_t::num; + return clock_freq; +#elif defined _WIN32 || defined WINCE LARGE_INTEGER freq; QueryPerformanceFrequency(&freq); return (double)freq.QuadPart; From fa613e393f96ef7de246a418761af72f36a0c390 Mon Sep 17 00:00:00 2001 From: Rong Mantle Bao Date: Sun, 10 Jul 2022 17:34:07 +0800 Subject: [PATCH 2/2] Read CV_CXX11 for C++11 detection --- modules/core/src/system.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index dda62fb89e..3b6877105d 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -301,14 +301,7 @@ DECLARE_CV_CPUID_X86 #endif #endif - -#if ((__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1800)) - #define HAVE_CXX11 1 -#else - #define HAVE_CXX11 0 -#endif - -#if HAVE_CXX11 +#if defined CV_CXX11 #include #endif @@ -850,7 +843,7 @@ bool useOptimized(void) int64 getTickCount(void) { -#if HAVE_CXX11 +#if defined CV_CXX11 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); return (int64)now.time_since_epoch().count(); #elif defined _WIN32 || defined WINCE @@ -872,7 +865,7 @@ int64 getTickCount(void) double getTickFrequency(void) { -#if HAVE_CXX11 +#if defined CV_CXX11 using clock_period_t = std::chrono::steady_clock::duration::period; double clock_freq = clock_period_t::den / clock_period_t::num; return clock_freq;