mirror of
https://github.com/NVIDIA/nvbench.git
synced 2026-03-14 20:27:24 +00:00
Add cpu_timer.
This commit is contained in:
35
nvbench/cpu_timer.cuh
Normal file
35
nvbench/cpu_timer.cuh
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace nvbench
|
||||
{
|
||||
|
||||
struct cpu_timer
|
||||
{
|
||||
cpu_timer() = default;
|
||||
|
||||
// move-only
|
||||
cpu_timer(const cpu_timer &) = delete;
|
||||
cpu_timer(cpu_timer &&) = default;
|
||||
cpu_timer &operator=(const cpu_timer &) = delete;
|
||||
cpu_timer &operator=(cpu_timer &&) = default;
|
||||
|
||||
void start() { m_start = std::chrono::high_resolution_clock::now(); }
|
||||
|
||||
void stop() { m_stop = std::chrono::high_resolution_clock::now(); }
|
||||
|
||||
double get_duration()
|
||||
{
|
||||
const auto duration = m_stop - m_start;
|
||||
const auto ns =
|
||||
std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
|
||||
return ns * (1e-9);
|
||||
}
|
||||
|
||||
private:
|
||||
std::chrono::high_resolution_clock::time_point m_start;
|
||||
std::chrono::high_resolution_clock::time_point m_stop;
|
||||
};
|
||||
|
||||
} // namespace nvbench
|
||||
@@ -2,6 +2,7 @@ set(test_srcs
|
||||
axes_metadata.cu
|
||||
benchmark.cu
|
||||
cuda_timer.cu
|
||||
cpu_timer.cu
|
||||
float64_axis.cu
|
||||
int64_axis.cu
|
||||
params.cu
|
||||
|
||||
22
testing/cpu_timer.cu
Normal file
22
testing/cpu_timer.cu
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <nvbench/cpu_timer.cuh>
|
||||
|
||||
#include "test_asserts.cuh"
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
void test_basic()
|
||||
{
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
nvbench::cpu_timer timer;
|
||||
|
||||
timer.start();
|
||||
std::this_thread::sleep_for(250ms);
|
||||
timer.stop();
|
||||
|
||||
ASSERT(timer.get_duration() > 0.25);
|
||||
ASSERT(timer.get_duration() < 0.50);
|
||||
}
|
||||
|
||||
int main() { test_basic(); }
|
||||
Reference in New Issue
Block a user