Compare commits
No commits in common. "32a1cd753354243560f9e9dfec20eae69fdad29f" and "ebf50df679ac26f68aa187cbe5f4823b456d786c" have entirely different histories.
32a1cd7533
...
ebf50df679
|
@ -5,8 +5,6 @@ project(
|
||||||
VERSION 0.1.0
|
VERSION 0.1.0
|
||||||
LANGUAGES CXX)
|
LANGUAGES CXX)
|
||||||
|
|
||||||
add_subdirectory(lib_vector_search)
|
|
||||||
|
|
||||||
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
|
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
|
||||||
|
|
||||||
include(ExportCompileCommands)
|
include(ExportCompileCommands)
|
||||||
|
@ -14,8 +12,6 @@ include(sccache)
|
||||||
|
|
||||||
add_executable(VectorSearch main.cpp)
|
add_executable(VectorSearch main.cpp)
|
||||||
|
|
||||||
target_link_libraries(VectorSearch lib_vector_search)
|
|
||||||
|
|
||||||
target_compile_features(VectorSearch PUBLIC cxx_std_20)
|
target_compile_features(VectorSearch PUBLIC cxx_std_20)
|
||||||
|
|
||||||
set_target_properties(
|
set_target_properties(
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
cmake_minimum_required(VERSION 3.20)
|
|
||||||
|
|
||||||
project(
|
|
||||||
lib_vector_search
|
|
||||||
VERSION 0.1.0
|
|
||||||
LANGUAGES CXX)
|
|
||||||
|
|
||||||
add_library(
|
|
||||||
lib_vector_search STATIC src/word_list_generator.cpp src/timer.cpp
|
|
||||||
src/linear_finder.cpp src/parallel_finder.cpp)
|
|
||||||
|
|
||||||
target_include_directories(lib_vector_search
|
|
||||||
PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
|
||||||
|
|
||||||
target_compile_features(lib_vector_search PUBLIC cxx_std_20)
|
|
|
@ -1,12 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
using std::string, std::string_view, std::vector;
|
|
||||||
|
|
||||||
class Finder {
|
|
||||||
public:
|
|
||||||
virtual ~Finder() = default;
|
|
||||||
virtual vector<const string *> find_prefix(string_view search_term) const = 0;
|
|
||||||
};
|
|
|
@ -1,12 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "finder.h"
|
|
||||||
|
|
||||||
class LinearFinder : public Finder {
|
|
||||||
private:
|
|
||||||
const vector<string> &word_list_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
LinearFinder(const vector<string> &word_list);
|
|
||||||
vector<const string *> find_prefix(string_view search_term) const override;
|
|
||||||
};
|
|
|
@ -1,13 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "finder.h"
|
|
||||||
|
|
||||||
class ParallelFinder : public Finder {
|
|
||||||
private:
|
|
||||||
const size_t thread_count_;
|
|
||||||
const vector<string> &word_list_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
ParallelFinder(const vector<string> &word_list, size_t thread_count);
|
|
||||||
vector<const string *> find_prefix(string_view search_term) const override;
|
|
||||||
};
|
|
|
@ -1,16 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class Timer {
|
|
||||||
private:
|
|
||||||
std::string name_;
|
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> start_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Timer(std::string_view name);
|
|
||||||
|
|
||||||
void start();
|
|
||||||
void stop();
|
|
||||||
};
|
|
|
@ -1,12 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class WordListGenerator {
|
|
||||||
private:
|
|
||||||
static const std::string charset_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
std::vector<std::string> generate();
|
|
||||||
};
|
|
|
@ -1,17 +0,0 @@
|
||||||
#include "linear_finder.h"
|
|
||||||
|
|
||||||
LinearFinder::LinearFinder(const vector<string> &word_list)
|
|
||||||
: word_list_(word_list) {}
|
|
||||||
|
|
||||||
vector<const string *>
|
|
||||||
LinearFinder::find_prefix(string_view search_term) const {
|
|
||||||
vector<const string *> matching_words;
|
|
||||||
|
|
||||||
for (const auto ¤t_word : word_list_) {
|
|
||||||
if (current_word.starts_with(search_term)) {
|
|
||||||
matching_words.push_back(¤t_word);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return matching_words;
|
|
||||||
}
|
|
|
@ -1,47 +0,0 @@
|
||||||
#include "parallel_finder.h"
|
|
||||||
|
|
||||||
#include <mutex>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
using std::mutex, std::thread, std::lock_guard;
|
|
||||||
|
|
||||||
ParallelFinder::ParallelFinder(const vector<string> &word_list,
|
|
||||||
size_t thread_count)
|
|
||||||
: word_list_(word_list), thread_count_(thread_count) {}
|
|
||||||
|
|
||||||
vector<const string *>
|
|
||||||
ParallelFinder::find_prefix(string_view search_term) const {
|
|
||||||
vector<const string *> result;
|
|
||||||
mutex result_mutex;
|
|
||||||
|
|
||||||
vector<thread> threads;
|
|
||||||
for (size_t thread_index = 0; thread_index < thread_count_; ++thread_index) {
|
|
||||||
const size_t first_word_index =
|
|
||||||
thread_index * (word_list_.size() / thread_count_);
|
|
||||||
const size_t last_word_index =
|
|
||||||
(thread_index == thread_count_ - 1)
|
|
||||||
? word_list_.size()
|
|
||||||
: (thread_index + 1) * (word_list_.size() / thread_count_);
|
|
||||||
|
|
||||||
threads.emplace_back(
|
|
||||||
[](const vector<string> &word_list, const string_view &search_term,
|
|
||||||
vector<const string *> &result, size_t start_index, size_t end_index,
|
|
||||||
mutex &result_mutex) {
|
|
||||||
for (size_t index = start_index; index < end_index; ++index) {
|
|
||||||
const auto ¤t_word = word_list[index];
|
|
||||||
if (current_word.starts_with(search_term)) {
|
|
||||||
const lock_guard<mutex> lock(result_mutex);
|
|
||||||
result.push_back(¤t_word);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
cref(word_list_), cref(search_term), ref(result), first_word_index,
|
|
||||||
last_word_index, ref(result_mutex));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &thread : threads) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
#include "timer.h"
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
Timer::Timer(std::string_view name) : name_(name) { start(); };
|
|
||||||
|
|
||||||
void Timer::start() { start_ = std::chrono::high_resolution_clock::now(); }
|
|
||||||
|
|
||||||
void Timer::stop() {
|
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
|
||||||
|
|
||||||
auto duration =
|
|
||||||
std::chrono::duration_cast<std::chrono::microseconds>(end - start_);
|
|
||||||
|
|
||||||
std::cout << name_ << " took " << duration << std::endl;
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
#include "word_list_generator.h"
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cmath>
|
|
||||||
#include <random>
|
|
||||||
|
|
||||||
const std::string WordListGenerator::charset_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
||||||
|
|
||||||
std::vector<std::string> WordListGenerator::generate() {
|
|
||||||
const size_t multiplier = 10;
|
|
||||||
|
|
||||||
std::vector<std::string> result;
|
|
||||||
result.reserve(multiplier * std::pow(charset_.length(), 4));
|
|
||||||
|
|
||||||
for (auto char_1 : charset_) {
|
|
||||||
for (auto char_2 : charset_) {
|
|
||||||
for (auto char_3 : charset_) {
|
|
||||||
for (auto char_4 : charset_) {
|
|
||||||
for (auto i = 0; i < multiplier; ++i) {
|
|
||||||
result.emplace_back(
|
|
||||||
std::initializer_list<char>({char_1, char_2, char_3, char_4}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::random_device random_device;
|
|
||||||
std::mt19937 random_number_generator(random_device());
|
|
||||||
|
|
||||||
std::shuffle(result.begin(), result.end(), random_number_generator);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
63
main.cpp
63
main.cpp
|
@ -1,66 +1,7 @@
|
||||||
#include "linear_finder.h"
|
|
||||||
#include "parallel_finder.h"
|
|
||||||
#include "timer.h"
|
|
||||||
#include "word_list_generator.h"
|
|
||||||
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#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;
|
|
||||||
|
|
||||||
Timer generator_timer("word list generator");
|
|
||||||
auto word_list = WordListGenerator().generate();
|
|
||||||
generator_timer.stop();
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
Timer constructor_timer("linear finder constructor");
|
|
||||||
LinearFinder linear_finder(word_list);
|
|
||||||
constructor_timer.stop();
|
|
||||||
|
|
||||||
Timer find_timer("linear finder find");
|
|
||||||
auto result = linear_finder.find_prefix("ABCD");
|
|
||||||
find_timer.stop();
|
|
||||||
|
|
||||||
cout << "result list is " << result.size() << " element(s) long" << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
Timer constructor_timer("parallel finder constructor");
|
|
||||||
ParallelFinder parallel_finder(word_list, thread_count);
|
|
||||||
constructor_timer.stop();
|
|
||||||
|
|
||||||
Timer find_timer("parallel finder find");
|
|
||||||
auto result = parallel_finder.find_prefix("ABCD");
|
|
||||||
find_timer.stop();
|
|
||||||
|
|
||||||
cout << "result list is " << result.size() << " element(s) long" << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
cout << "\n== VectorSearch ==" << endl;
|
std::cout << "VectorSearch" << std::endl;
|
||||||
|
|
||||||
auto word_list = generate_word_list();
|
return 0;
|
||||||
|
|
||||||
test_linear_finder(word_list);
|
|
||||||
test_parallel_finder(word_list);
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue