diff --git a/lib_vector_search/include/timer.h b/lib_vector_search/include/timer.h index 45226e4..c04724f 100644 --- a/lib_vector_search/include/timer.h +++ b/lib_vector_search/include/timer.h @@ -1,16 +1,17 @@ #pragma once #include -#include class Timer { private: - std::string name_; std::chrono::time_point start_; + std::chrono::time_point end_; public: - Timer(std::string_view name); + Timer(); void start(); void stop(); + + long us() const; }; diff --git a/lib_vector_search/src/timer.cpp b/lib_vector_search/src/timer.cpp index 43987b5..960868c 100644 --- a/lib_vector_search/src/timer.cpp +++ b/lib_vector_search/src/timer.cpp @@ -1,17 +1,14 @@ #include "timer.h" #include -#include -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(end - start_); - - std::cout << name_ << " took " << duration << std::endl; +long Timer::us() const { + return std::chrono::duration_cast(end_ - start_) + .count(); } diff --git a/vector_search_cli/main.cpp b/vector_search_cli/main.cpp index f9c356e..f0377f9 100644 --- a/vector_search_cli/main.cpp +++ b/vector_search_cli/main.cpp @@ -13,10 +13,11 @@ using std::string, std::string_view, std::vector, std::thread, std::cout, vector 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 generate_word_list() { void test_linear_finder(const vector &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 &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; }