2024-03-19 16:10:48 +00:00
|
|
|
#include "linear_finder.h"
|
|
|
|
#include "parallel_finder.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "word_list_generator.h"
|
|
|
|
|
|
|
|
#include <cstdlib>
|
2024-03-18 11:12:08 +00:00
|
|
|
#include <iostream>
|
2024-03-19 16:10:48 +00:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
using std::string, std::string_view, std::vector, std::thread, std::cout,
|
|
|
|
std::endl;
|
|
|
|
|
|
|
|
vector<string> generate_word_list() {
|
|
|
|
cout << "\ngenerating word list" << endl;
|
|
|
|
|
2024-03-20 08:58:40 +00:00
|
|
|
Timer generator_timer;
|
2024-03-19 16:10:48 +00:00
|
|
|
auto word_list = WordListGenerator().generate();
|
|
|
|
generator_timer.stop();
|
|
|
|
|
2024-03-20 09:13:08 +00:00
|
|
|
cout << "word list generator took " << generator_timer << endl;
|
2024-03-19 16:10:48 +00:00
|
|
|
cout << "word list is " << word_list.size() << " element(s) long" << endl;
|
|
|
|
|
|
|
|
return word_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_linear_finder(const vector<string> &word_list) {
|
|
|
|
cout << "\nrunning linear finder" << endl;
|
|
|
|
|
2024-03-20 08:58:40 +00:00
|
|
|
Timer constructor_timer;
|
2024-03-19 16:10:48 +00:00
|
|
|
LinearFinder linear_finder(word_list);
|
|
|
|
constructor_timer.stop();
|
|
|
|
|
2024-03-20 08:58:40 +00:00
|
|
|
Timer find_timer;
|
2024-03-19 16:10:48 +00:00
|
|
|
auto result = linear_finder.find_prefix("ABCD");
|
|
|
|
find_timer.stop();
|
|
|
|
|
2024-03-20 09:13:08 +00:00
|
|
|
cout << "linear finder took " << find_timer << endl;
|
2024-03-20 12:25:28 +00:00
|
|
|
cout << "result list is " << std::distance(result.cbegin(), result.cend())
|
|
|
|
<< " element(s) long" << endl;
|
2024-03-19 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_parallel_finder(const vector<string> &word_list) {
|
|
|
|
cout << "\nrunning parallel finder" << endl;
|
|
|
|
|
|
|
|
const size_t thread_count = thread::hardware_concurrency();
|
|
|
|
|
|
|
|
cout << "using " << thread_count << " threads" << endl;
|
|
|
|
|
2024-03-20 08:58:40 +00:00
|
|
|
Timer constructor_timer;
|
2024-03-19 16:10:48 +00:00
|
|
|
ParallelFinder parallel_finder(word_list, thread_count);
|
|
|
|
constructor_timer.stop();
|
|
|
|
|
2024-03-20 08:58:40 +00:00
|
|
|
Timer find_timer;
|
2024-03-19 16:10:48 +00:00
|
|
|
auto result = parallel_finder.find_prefix("ABCD");
|
|
|
|
find_timer.stop();
|
|
|
|
|
2024-03-20 09:13:08 +00:00
|
|
|
cout << "parallel finder took " << find_timer << endl;
|
2024-03-20 12:25:28 +00:00
|
|
|
cout << "result list is " << std::distance(result.cbegin(), result.cend())
|
|
|
|
<< " element(s) long" << endl;
|
2024-03-19 16:10:48 +00:00
|
|
|
}
|
2024-03-18 11:12:08 +00:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2024-03-19 16:10:48 +00:00
|
|
|
cout << "\n== VectorSearch ==" << endl;
|
|
|
|
|
|
|
|
auto word_list = generate_word_list();
|
|
|
|
|
|
|
|
test_linear_finder(word_list);
|
|
|
|
test_parallel_finder(word_list);
|
2024-03-18 11:12:08 +00:00
|
|
|
|
2024-03-19 16:10:48 +00:00
|
|
|
return EXIT_SUCCESS;
|
2024-03-18 11:12:08 +00:00
|
|
|
}
|