time: use std::chrono for nanoseconds query
This commit is contained in:
parent
f644c5dec4
commit
678e12216b
27
time.cpp
27
time.cpp
@ -11,24 +11,37 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* 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 "log.hpp"
|
||||
#include "platform.hpp"
|
||||
#include "cast.hpp"
|
||||
#include "./debug.hpp"
|
||||
#include "./log.hpp"
|
||||
#include "./platform.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 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 ():
|
||||
time { util::nanoseconds (), util::nanoseconds () }
|
||||
|
15
time.hpp
15
time.hpp
@ -11,21 +11,26 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_TIME_HPP
|
||||
#define __UTIL_TIME_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "stats.hpp"
|
||||
#include "./stats.hpp"
|
||||
|
||||
namespace util {
|
||||
// ------------------------------------------------------------------------
|
||||
uint64_t nanoseconds (void);
|
||||
void sleep (uint64_t ns);
|
||||
uintmax_t nanoseconds (void);
|
||||
|
||||
template <typename T>
|
||||
void sleep (std::chrono::duration<T,std::nano>);
|
||||
|
||||
void sleep (uint64_t ns);
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
class delta_clock {
|
||||
@ -84,4 +89,6 @@ namespace util {
|
||||
};
|
||||
}
|
||||
|
||||
#include "./time.ipp"
|
||||
|
||||
#endif // __UTIL_TIME_HPP
|
||||
|
28
time.ipp
Normal file
28
time.ipp
Normal 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
|
@ -11,31 +11,16 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* 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>
|
||||
|
||||
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<uint64_t> (t.tv_sec) * SECOND + static_cast<uint64_t> (t.tv_nsec);
|
||||
}
|
||||
|
||||
static constexpr uint64_t SECOND = 1'000'000'000UL;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
|
@ -11,37 +11,16 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "time.hpp"
|
||||
|
||||
#include "platform.hpp"
|
||||
|
||||
#if !defined(PLATFORM_WIN32)
|
||||
#error
|
||||
#endif
|
||||
|
||||
#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
|
||||
util::sleep (uint64_t ns)
|
||||
{
|
||||
Sleep (ns / MILLISECOND);
|
||||
Sleep (ns / 1'000'000UL);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user