refactor: extract lib_timer
This commit is contained in:
parent
14dbf00775
commit
10b19749f7
8 changed files with 433 additions and 4 deletions
|
@ -9,8 +9,6 @@ add_library(
|
|||
vector_search STATIC
|
||||
src/word_list_generator.cpp
|
||||
include/word_list_generator.h
|
||||
src/timer.cpp
|
||||
include/timer.h
|
||||
src/linear_finder.cpp
|
||||
include/linear_finder.h
|
||||
src/sorted_linear_finder.cpp
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <ostream>
|
||||
|
||||
class Timer {
|
||||
private:
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> start_;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> end_;
|
||||
|
||||
public:
|
||||
Timer();
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
long us() const;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Timer &timer);
|
|
@ -1,32 +0,0 @@
|
|||
#include "timer.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
Timer::Timer() { start(); };
|
||||
|
||||
void Timer::start() { start_ = std::chrono::high_resolution_clock::now(); }
|
||||
|
||||
void Timer::stop() { end_ = std::chrono::high_resolution_clock::now(); }
|
||||
|
||||
long Timer::us() const {
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(end_ - start_)
|
||||
.count();
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Timer &timer) {
|
||||
long time = timer.us();
|
||||
|
||||
if (time >= 1e6) {
|
||||
os << time / static_cast<long>(1e6) << " s ";
|
||||
time %= static_cast<long>(1e6);
|
||||
}
|
||||
|
||||
if (time >= 1e3) {
|
||||
os << time / static_cast<long>(1e3) << " ms ";
|
||||
time %= static_cast<long>(1e3);
|
||||
}
|
||||
|
||||
os << time << " µs";
|
||||
|
||||
return os;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue