- added shuffling

- linear Search
- simple testrunner for time-measurement
This commit is contained in:
Karsten Laux 2021-12-15 16:25:15 +01:00
parent 25555097ba
commit 60ac1ce373
3 changed files with 46 additions and 23 deletions

View File

@ -6,6 +6,6 @@ cmake_minimum_required (VERSION 3.8)
project ("ParallelSearch")
# Fügen Sie der ausführbaren Datei dieses Projekts eine Quelle hinzu.
add_executable (ParallelSearch "main.cpp" "SearchJob.hpp")
add_executable (ParallelSearch "main.cpp" "SearchJob.hpp" "TestRunner.hpp")
# TODO: Fügen Sie bei Bedarf Tests hinzu, und installieren Sie Ziele.

18
TestRunner.hpp Normal file
View File

@ -0,0 +1,18 @@
#ifndef TESTRUNNER_HPP
#define TESTRUNNER_HPP
#include <chrono>
template <typename Func, typename Arg1>
double TestRunner(Func func, Arg1& arg1)
{
std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
func(arg1);
std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
return time_span.count();
}
#endif

View File

@ -1,15 +1,18 @@
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
#include <algorithm>
#include <random>
#include "SearchJob.hpp"
#include "TestRunner.hpp"
using namespace std::chrono;
// Type of our hay stack: a list of strings
typedef std::vector<std::string> WordList;
typedef SearchJob<WordList::const_iterator> WordSearchJob;
void create_testdata(WordList& words)
{
const char start = 'A';
@ -23,36 +26,38 @@ void create_testdata(WordList& words)
words.push_back(std::string(str, 4));
}
void shuffle_testdata(WordList& words)
{
auto rng = std::default_random_engine{};
std::shuffle(words.begin(), words.end(), rng);
}
void run_wordsearch(WordSearchJob& job)
{
job.run();
}
int main()
{
std::cout << "Hello World." << std::endl;
double time_span;
WordList test_data;
std::cout << "Hello World." << std::endl;
{
steady_clock::time_point t1 = steady_clock::now();
create_testdata(test_data);
steady_clock::time_point t2 = steady_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
time_span = TestRunner(create_testdata, test_data);
std::cout << "It took me " << time_span << " seconds to create " << test_data.size() << " words." << std::endl;
std::cout << "It took me " << time_span.count() << " seconds to create " << test_data.size() << " words." << std::endl;
}
time_span = TestRunner(shuffle_testdata, test_data);
std::cout << "It took me " << time_span << " seconds to shuffle " << test_data.size() << " words." << std::endl;
SearchJob<WordList::const_iterator> job(test_data.begin(), test_data.end(), "ABC");
time_span = TestRunner(run_wordsearch, job);
std::cout << "It took me " << time_span << " seconds to find " << job.GetResult().size() << " matches." << std::endl;
{
SearchJob<WordList::const_iterator> job(test_data.begin(), test_data.end(), "ABC");
steady_clock::time_point t1 = steady_clock::now();
job.run();
steady_clock::time_point t2 = steady_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
std::cout << "It took me " << time_span.count() << " seconds to find " << job.GetResult().size() << " matches." << std::endl;
}
std::cout << std::endl;
return 0;
}