From 678e12216b49f9b5bdf95f20dbb43d5cb761cb31 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 5 Apr 2016 11:02:54 +1000 Subject: [PATCH] time: use std::chrono for nanoseconds query --- time.cpp | 27 ++++++++++++++++++++------- time.hpp | 15 +++++++++++---- time.ipp | 28 ++++++++++++++++++++++++++++ time_posix.cpp | 23 ++++------------------- time_win32.cpp | 25 ++----------------------- 5 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 time.ipp diff --git a/time.cpp b/time.cpp index 5e61ff98..42ff1966 100644 --- a/time.cpp +++ b/time.cpp @@ -11,24 +11,37 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010 Danny Robson + * Copyright 2010-2016 Danny Robson */ -#include "time.hpp" +#include "./time.hpp" -#include "debug.hpp" -#include "log.hpp" -#include "platform.hpp" -#include "cast.hpp" +#include "./debug.hpp" +#include "./log.hpp" +#include "./platform.hpp" +#include "./cast.hpp" +#include -using namespace util; +using util::delta_clock; /////////////////////////////////////////////////////////////////////////////// static const uint64_t SECOND = 1'000'000'000UL; static const uint64_t MILLISECOND = 1'000'000UL; +/////////////////////////////////////////////////////////////////////////////// +uintmax_t +util::nanoseconds (void) +{ + return std::chrono::duration_cast< + std::chrono::duration + > ( + std::chrono::high_resolution_clock::now ().time_since_epoch () + ).count (); +} + + /////////////////////////////////////////////////////////////////////////////// delta_clock::delta_clock (): time { util::nanoseconds (), util::nanoseconds () } diff --git a/time.hpp b/time.hpp index 89cd6d98..a2c3d800 100644 --- a/time.hpp +++ b/time.hpp @@ -11,21 +11,26 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010 Danny Robson + * Copyright 2010-2016 Danny Robson */ #ifndef __UTIL_TIME_HPP #define __UTIL_TIME_HPP +#include #include #include -#include "stats.hpp" +#include "./stats.hpp" namespace util { // ------------------------------------------------------------------------ - uint64_t nanoseconds (void); - void sleep (uint64_t ns); + uintmax_t nanoseconds (void); + + template + void sleep (std::chrono::duration); + + void sleep (uint64_t ns); // ------------------------------------------------------------------------ class delta_clock { @@ -84,4 +89,6 @@ namespace util { }; } +#include "./time.ipp" + #endif // __UTIL_TIME_HPP diff --git a/time.ipp b/time.ipp new file mode 100644 index 00000000..a1c1604b --- /dev/null +++ b/time.ipp @@ -0,0 +1,28 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2016 Danny Robson + */ + +#ifndef __UTIL_TIME_IPP +#define __UTIL_TIME_IPP + +template +void +util::sleep (std::chrono::duration dt) +{ + auto nano = std::chrono::duration_cast (dt); + sleep (nano.count ()); +} + +#endif diff --git a/time_posix.cpp b/time_posix.cpp index effd2bf4..9812c2d4 100644 --- a/time_posix.cpp +++ b/time_posix.cpp @@ -11,31 +11,16 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010 Danny Robson + * Copyright 2010-2016 Danny Robson */ -#include "time.hpp" +#include "./time.hpp" -#include "cast.hpp" +#include "./cast.hpp" #include -static const uint64_t SECOND = 1'000'000'000UL; -static const uint64_t MILLISECOND = 1'000'000UL; - -/////////////////////////////////////////////////////////////////////////////// -uint64_t -util::nanoseconds (void) -{ - struct timespec t; - clock_gettime (CLOCK_MONOTONIC, &t); - - CHECK_GT (t.tv_sec, 0); - CHECK_GT (t.tv_nsec, 0); - - return static_cast (t.tv_sec) * SECOND + static_cast (t.tv_nsec); -} - +static constexpr uint64_t SECOND = 1'000'000'000UL; /////////////////////////////////////////////////////////////////////////////// void diff --git a/time_win32.cpp b/time_win32.cpp index 93a11733..809bd87b 100644 --- a/time_win32.cpp +++ b/time_win32.cpp @@ -11,37 +11,16 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010 Danny Robson + * Copyright 2010-2016 Danny Robson */ #include "time.hpp" -#include "platform.hpp" - -#if !defined(PLATFORM_WIN32) -#error -#endif - #include -static const uint64_t SECOND = 1'000'000'000UL; -static const uint64_t MILLISECOND = 1'000'000UL; - -/////////////////////////////////////////////////////////////////////////////// -uint64_t -util::nanoseconds (void) -{ - LARGE_INTEGER freq, count; - QueryPerformanceFrequency (&freq); - QueryPerformanceCounter (&count); - - return count.QuadPart * SECOND / freq.QuadPart; -} - - /////////////////////////////////////////////////////////////////////////////// void util::sleep (uint64_t ns) { - Sleep (ns / MILLISECOND); + Sleep (ns / 1'000'000UL); }