From 60ac1ce3730947371339b3e9f70d7ef929728811 Mon Sep 17 00:00:00 2001 From: karsten Date: Wed, 15 Dec 2021 16:25:15 +0100 Subject: [PATCH] - added shuffling - linear Search - simple testrunner for time-measurement --- CMakeLists.txt | 2 +- TestRunner.hpp | 18 ++++++++++++++++++ main.cpp | 49 +++++++++++++++++++++++++++---------------------- 3 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 TestRunner.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c452f4d..af5f97e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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. diff --git a/TestRunner.hpp b/TestRunner.hpp new file mode 100644 index 0000000..ea5ec60 --- /dev/null +++ b/TestRunner.hpp @@ -0,0 +1,18 @@ +#ifndef TESTRUNNER_HPP +#define TESTRUNNER_HPP + +#include + +template +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 time_span = std::chrono::duration_cast>(t2 - t1); + + return time_span.count(); +} + + +#endif diff --git a/main.cpp b/main.cpp index 0dbba8b..dba0dc0 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,18 @@ #include #include #include -#include +#include +#include #include "SearchJob.hpp" +#include "TestRunner.hpp" -using namespace std::chrono; // Type of our hay stack: a list of strings typedef std::vector WordList; +typedef SearchJob 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 time_span = duration_cast>(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 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 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 time_span = duration_cast>(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; }