time: use std::chrono for nanoseconds query

This commit is contained in:
Danny Robson 2016-04-05 11:02:54 +10:00
parent f644c5dec4
commit 678e12216b
5 changed files with 65 additions and 53 deletions

View File

@ -11,24 +11,37 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2010 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#include "time.hpp" #include "./time.hpp"
#include "debug.hpp" #include "./debug.hpp"
#include "log.hpp" #include "./log.hpp"
#include "platform.hpp" #include "./platform.hpp"
#include "cast.hpp" #include "./cast.hpp"
#include <chrono>
using namespace util; using util::delta_clock;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static const uint64_t SECOND = 1'000'000'000UL; static const uint64_t SECOND = 1'000'000'000UL;
static const uint64_t MILLISECOND = 1'000'000UL; static const uint64_t MILLISECOND = 1'000'000UL;
///////////////////////////////////////////////////////////////////////////////
uintmax_t
util::nanoseconds (void)
{
return std::chrono::duration_cast<
std::chrono::duration<uintmax_t, std::nano>
> (
std::chrono::high_resolution_clock::now ().time_since_epoch ()
).count ();
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
delta_clock::delta_clock (): delta_clock::delta_clock ():
time { util::nanoseconds (), util::nanoseconds () } time { util::nanoseconds (), util::nanoseconds () }

View File

@ -11,21 +11,26 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2010 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_TIME_HPP #ifndef __UTIL_TIME_HPP
#define __UTIL_TIME_HPP #define __UTIL_TIME_HPP
#include <chrono>
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include "stats.hpp" #include "./stats.hpp"
namespace util { namespace util {
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
uint64_t nanoseconds (void); uintmax_t nanoseconds (void);
void sleep (uint64_t ns);
template <typename T>
void sleep (std::chrono::duration<T,std::nano>);
void sleep (uint64_t ns);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
class delta_clock { class delta_clock {
@ -84,4 +89,6 @@ namespace util {
}; };
} }
#include "./time.ipp"
#endif // __UTIL_TIME_HPP #endif // __UTIL_TIME_HPP

28
time.ipp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#ifndef __UTIL_TIME_IPP
#define __UTIL_TIME_IPP
template <typename T>
void
util::sleep (std::chrono::duration<T,std::nano> dt)
{
auto nano = std::chrono::duration_cast<std::chrono::nanoseconds> (dt);
sleep (nano.count ());
}
#endif

View File

@ -11,31 +11,16 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2010 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#include "time.hpp" #include "./time.hpp"
#include "cast.hpp" #include "./cast.hpp"
#include <ctime> #include <ctime>
static const uint64_t SECOND = 1'000'000'000UL; static constexpr 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<uint64_t> (t.tv_sec) * SECOND + static_cast<uint64_t> (t.tv_nsec);
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void void

View File

@ -11,37 +11,16 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2010 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#include "time.hpp" #include "time.hpp"
#include "platform.hpp"
#if !defined(PLATFORM_WIN32)
#error
#endif
#include <windows.h> #include <windows.h>
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 void
util::sleep (uint64_t ns) util::sleep (uint64_t ns)
{ {
Sleep (ns / MILLISECOND); Sleep (ns / 1'000'000UL);
} }