- added shuffling
- linear Search - simple testrunner for time-measurement
This commit is contained in:
parent
25555097ba
commit
60ac1ce373
@ -6,6 +6,6 @@ cmake_minimum_required (VERSION 3.8)
|
|||||||
project ("ParallelSearch")
|
project ("ParallelSearch")
|
||||||
|
|
||||||
# Fügen Sie der ausführbaren Datei dieses Projekts eine Quelle hinzu.
|
# 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.
|
# TODO: Fügen Sie bei Bedarf Tests hinzu, und installieren Sie Ziele.
|
||||||
|
|||||||
18
TestRunner.hpp
Normal file
18
TestRunner.hpp
Normal 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
|
||||||
47
main.cpp
47
main.cpp
@ -1,15 +1,18 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <chrono>
|
#include <algorithm>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
#include "SearchJob.hpp"
|
#include "SearchJob.hpp"
|
||||||
|
#include "TestRunner.hpp"
|
||||||
|
|
||||||
using namespace std::chrono;
|
|
||||||
|
|
||||||
// Type of our hay stack: a list of strings
|
// Type of our hay stack: a list of strings
|
||||||
typedef std::vector<std::string> WordList;
|
typedef std::vector<std::string> WordList;
|
||||||
|
|
||||||
|
typedef SearchJob<WordList::const_iterator> WordSearchJob;
|
||||||
|
|
||||||
void create_testdata(WordList& words)
|
void create_testdata(WordList& words)
|
||||||
{
|
{
|
||||||
const char start = 'A';
|
const char start = 'A';
|
||||||
@ -23,36 +26,38 @@ void create_testdata(WordList& words)
|
|||||||
words.push_back(std::string(str, 4));
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout << "Hello World." << std::endl;
|
double time_span;
|
||||||
|
|
||||||
|
|
||||||
WordList test_data;
|
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);
|
|
||||||
|
|
||||||
std::cout << "It took me " << time_span.count() << " seconds to create " << test_data.size() << " words." << std::endl;
|
time_span = TestRunner(create_testdata, test_data);
|
||||||
}
|
std::cout << "It took me " << time_span << " 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");
|
||||||
SearchJob<WordList::const_iterator> job(test_data.begin(), test_data.end(), "ABC");
|
|
||||||
steady_clock::time_point t1 = steady_clock::now();
|
time_span = TestRunner(run_wordsearch, job);
|
||||||
job.run();
|
std::cout << "It took me " << time_span << " seconds to find " << job.GetResult().size() << " matches." << std::endl;
|
||||||
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;
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user