From 6a67e5685f1e4bda9ca7a84e0ece9f08068ac5b2 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 20 Mar 2024 16:37:00 +0100 Subject: [PATCH] refactor: extract test runs --- vector_search_cli/main.cpp | 59 +++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/vector_search_cli/main.cpp b/vector_search_cli/main.cpp index 0d31e5b..4deb970 100644 --- a/vector_search_cli/main.cpp +++ b/vector_search_cli/main.cpp @@ -1,3 +1,4 @@ +#include "grouped_finder.h" #include "linear_finder.h" #include "parallel_finder.h" #include "timer.h" @@ -24,36 +25,37 @@ vector generate_word_list() { return word_list; } +void test_finder_search(Finder &finder, std::string_view name, + std::string_view search_term) { + Timer find_timer; + auto result = finder.find_prefix(search_term); + find_timer.stop(); + + cout << name << "(" << search_term << ") took " << find_timer << endl; + cout << "result list is " << std::distance(result.cbegin(), result.cend()) + << " element(s) long" << endl; +} + +void test_finder(Finder &finder, std::string_view name) { + for (const auto &search_term : {"A", "AB", "ABC", "ABCD"}) { + test_finder_search(finder, name, search_term); + } +} + void test_linear_finder(const vector &word_list) { cout << "\nrunning linear finder" << endl; LinearFinder linear_finder(word_list); - Timer find_timer; - auto result = linear_finder.find_prefix("ABCD"); - find_timer.stop(); - - cout << "linear finder took " << find_timer << endl; - cout << "result list is " << std::distance(result.cbegin(), result.cend()) - << " element(s) long" << endl; + test_finder(linear_finder, "linear finder"); } void test_parallel_finder(const vector &word_list) { cout << "\nrunning parallel finder" << endl; - const size_t thread_count = thread::hardware_concurrency(); + ParallelFinder parallel_finder(word_list); - cout << "using " << thread_count << " threads" << endl; - - ParallelFinder parallel_finder(word_list, thread_count); - - Timer find_timer; - auto result = parallel_finder.find_prefix("ABCD"); - find_timer.stop(); - - cout << "parallel finder took " << find_timer << endl; - cout << "result list is " << std::distance(result.cbegin(), result.cend()) - << " element(s) long" << endl; + test_finder(parallel_finder, "parallel finder"); } void test_tree_finder(const vector &word_list) { @@ -62,16 +64,20 @@ void test_tree_finder(const vector &word_list) { Timer constructor_timer; TreeFinder tree_finder(word_list); constructor_timer.stop(); - cout << "tree finder constructor took " << constructor_timer << endl; - Timer find_timer; - auto result = tree_finder.find_prefix("ABCD"); - find_timer.stop(); + test_finder(tree_finder, "tree finder"); +} - cout << "tree finder took " << find_timer << endl; - cout << "result list is " << std::distance(result.cbegin(), result.cend()) - << " element(s) long" << endl; +void test_grouped_finder(const vector &word_list) { + cout << "\nrunning grouped finder" << endl; + + Timer constructor_timer; + GroupedFinder grouped_finder(word_list); + constructor_timer.stop(); + cout << "grouped finder constructor took " << constructor_timer << endl; + + test_finder(grouped_finder, "grouped_finder"); } int main(int argc, char *argv[]) { @@ -82,6 +88,7 @@ int main(int argc, char *argv[]) { test_linear_finder(word_list); test_parallel_finder(word_list); test_tree_finder(word_list); + test_grouped_finder(word_list); return EXIT_SUCCESS; }