refactor: return numerical value from timer
This commit is contained in:
parent
f7634bdd95
commit
64ae4c62a9
@ -1,16 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
|
||||
class Timer {
|
||||
private:
|
||||
std::string name_;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> start_;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> end_;
|
||||
|
||||
public:
|
||||
Timer(std::string_view name);
|
||||
Timer();
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
long us() const;
|
||||
};
|
||||
|
@ -1,17 +1,14 @@
|
||||
#include "timer.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
Timer::Timer(std::string_view name) : name_(name) { start(); };
|
||||
Timer::Timer() { start(); };
|
||||
|
||||
void Timer::start() { start_ = std::chrono::high_resolution_clock::now(); }
|
||||
|
||||
void Timer::stop() {
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
void Timer::stop() { end_ = std::chrono::high_resolution_clock::now(); }
|
||||
|
||||
auto duration =
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(end - start_);
|
||||
|
||||
std::cout << name_ << " took " << duration << std::endl;
|
||||
long Timer::us() const {
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(end_ - start_)
|
||||
.count();
|
||||
}
|
||||
|
@ -13,10 +13,11 @@ using std::string, std::string_view, std::vector, std::thread, std::cout,
|
||||
vector<string> generate_word_list() {
|
||||
cout << "\ngenerating word list" << endl;
|
||||
|
||||
Timer generator_timer("word list generator");
|
||||
Timer generator_timer;
|
||||
auto word_list = WordListGenerator().generate();
|
||||
generator_timer.stop();
|
||||
|
||||
cout << "word list generator took " << generator_timer.us() << " µs" << endl;
|
||||
cout << "word list is " << word_list.size() << " element(s) long" << endl;
|
||||
|
||||
return word_list;
|
||||
@ -25,14 +26,15 @@ vector<string> generate_word_list() {
|
||||
void test_linear_finder(const vector<string> &word_list) {
|
||||
cout << "\nrunning linear finder" << endl;
|
||||
|
||||
Timer constructor_timer("linear finder constructor");
|
||||
Timer constructor_timer;
|
||||
LinearFinder linear_finder(word_list);
|
||||
constructor_timer.stop();
|
||||
|
||||
Timer find_timer("linear finder find");
|
||||
Timer find_timer;
|
||||
auto result = linear_finder.find_prefix("ABCD");
|
||||
find_timer.stop();
|
||||
|
||||
cout << "linear finder took " << find_timer.us() << " µs" << endl;
|
||||
cout << "result list is " << result.size() << " element(s) long" << endl;
|
||||
}
|
||||
|
||||
@ -43,14 +45,15 @@ void test_parallel_finder(const vector<string> &word_list) {
|
||||
|
||||
cout << "using " << thread_count << " threads" << endl;
|
||||
|
||||
Timer constructor_timer("parallel finder constructor");
|
||||
Timer constructor_timer;
|
||||
ParallelFinder parallel_finder(word_list, thread_count);
|
||||
constructor_timer.stop();
|
||||
|
||||
Timer find_timer("parallel finder find");
|
||||
Timer find_timer;
|
||||
auto result = parallel_finder.find_prefix("ABCD");
|
||||
find_timer.stop();
|
||||
|
||||
cout << "parallel finder took " << find_timer.us() << " µs" << endl;
|
||||
cout << "result list is " << result.size() << " element(s) long" << endl;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user