Add cpu_timer.

This commit is contained in:
Allison Vacanti
2020-12-29 23:51:09 -05:00
parent b07ffafff4
commit 8c0b8e3423
3 changed files with 58 additions and 0 deletions

35
nvbench/cpu_timer.cuh Normal file
View 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

View File

@@ -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
View 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(); }