refactor: extract test runs
This commit is contained in:
parent
21ca48b9c2
commit
6a67e5685f
@ -1,3 +1,4 @@
|
||||
#include "grouped_finder.h"
|
||||
#include "linear_finder.h"
|
||||
#include "parallel_finder.h"
|
||||
#include "timer.h"
|
||||
@ -24,36 +25,37 @@ vector<string> 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<string> &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<string> &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<string> &word_list) {
|
||||
@ -62,16 +64,20 @@ void test_tree_finder(const vector<string> &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<string> &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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user