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