From ccb01738778411b390efbfda54cb1223135ecdc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 27 Nov 2016 17:54:55 +0100 Subject: [PATCH] refactored code intho thread_pool.hpp & test.cpp for testing. --- .gitignore | 1 + CMakeLists.txt | 2 +- test.cpp | 78 +++++++++++++++++++++++++++++++++++ main.cpp => thread_pool.hpp | 81 +------------------------------------ 4 files changed, 81 insertions(+), 81 deletions(-) create mode 100644 test.cpp rename main.cpp => thread_pool.hpp (70%) diff --git a/.gitignore b/.gitignore index 182bfcd..3cb60c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea .DS_Store build +cmake-build-debug diff --git a/CMakeLists.txt b/CMakeLists.txt index d0a6bb0..eb022d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,6 @@ project(thread_pool) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -pthread") -set(SOURCE_FILES main.cpp) +set(SOURCE_FILES thread_pool.hpp test.cpp) add_executable(thread_pool ${SOURCE_FILES}) diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..1655a11 --- /dev/null +++ b/test.cpp @@ -0,0 +1,78 @@ +#include "thread_pool.hpp" +#include +#include +#include + + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////// functions for testing & test zone //////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////// + +boost::multiprecision::cpp_int mp_factorial(unsigned long long int number) +{ + boost::multiprecision::cpp_int num = number; + for (unsigned long long int i = number; i > 1; --i) + { + num *=(--number); + } + + return num; +} + +void simpleTask() +{ + std::this_thread::sleep_for(std::chrono::milliseconds(1)); +} + +void function_vs_packaged_task_test(thread_pool &tp, const unsigned short TIMES = 10000) +{ + namespace ch = std::chrono; + namespace mp = boost::multiprecision; + + + auto fstart = ch::high_resolution_clock::now(); + for (int j = 0; j < TIMES; ++j) + { + tp.add_task(simpleTask); + } + auto fdur = ch::high_resolution_clock::now() - fstart; + + auto pt = std::packaged_task(simpleTask); + auto pstart = ch::high_resolution_clock::now(); + for (int j = 0; j < TIMES; ++j) + { + tp.add_task(pt); + } + auto pdur = ch::high_resolution_clock::now() - pstart; + + std::cout << "Function: " << ch::duration_cast(fdur).count() << + std::endl << "Packaged task: " << ch::duration_cast(pdur).count() << std::endl; +} + +void factorial_test(thread_pool& tp, unsigned long long number) +{ + std::packaged_task pt(std::bind(mp_factorial, number-1)); + tp.add_task(pt); + auto fu1 = tp.add_task(mp_factorial, number); + auto fu2 = pt.get_future(); + + std::cout << "Results for n & n-1: " << fu1.get() << " " << fu2.get() << std::endl; + std::cout << "With " << tp.get_thread_num() << " threads" << std::endl; + std::cout << "Type of future: " << boost::typeindex::type_id_with_cvr().pretty_name() << std::endl; +} + +void boost_vs_me_test(thread_pool& tp) +{ /* TODO */ } + + +int main(void) +{ + thread_pool tp(4); + + factorial_test(tp, 6); + function_vs_packaged_task_test(tp, 10000); + + + return 0; +} \ No newline at end of file diff --git a/main.cpp b/thread_pool.hpp similarity index 70% rename from main.cpp rename to thread_pool.hpp index cc98c24..d848419 100644 --- a/main.cpp +++ b/thread_pool.hpp @@ -1,3 +1,4 @@ +#pragma once // // main.cpp // thread_pool v0.2a @@ -27,7 +28,6 @@ // /* necessary includes */ -#include #include #include #include @@ -38,13 +38,6 @@ #include #include -/* for the tests */ -#include -#include -//#include -//#include -//#include - class thread_pool @@ -203,75 +196,3 @@ void thread_pool::loop() fun(); } } - -/////////////////////////////////////////////////////////////////////////////////////////////////////////// -//////////////////// functions for testing & test zone //////////////////// -/////////////////////////////////////////////////////////////////////////////////////////////////////////// - -boost::multiprecision::cpp_int mp_factorial(unsigned long long int number) -{ - boost::multiprecision::cpp_int num = number; - for (unsigned long long int i = number; i > 1; --i) - { - num *=(--number); - } - - return num; -} - -void simpleTask() -{ - std::this_thread::sleep_for(std::chrono::milliseconds(1)); -} - -void function_vs_packaged_task_test(thread_pool &tp, const unsigned short TIMES = 10000) -{ - namespace ch = std::chrono; - namespace mp = boost::multiprecision; - - - auto fstart = ch::high_resolution_clock::now(); - for (int j = 0; j < TIMES; ++j) - { - tp.add_task(simpleTask); - } - auto fdur = ch::high_resolution_clock::now() - fstart; - - auto pt = std::packaged_task(simpleTask); - auto pstart = ch::high_resolution_clock::now(); - for (int j = 0; j < TIMES; ++j) - { - tp.add_task(pt); - } - auto pdur = ch::high_resolution_clock::now() - pstart; - - std::cout << "Function: " << ch::duration_cast(fdur).count() << - std::endl << "Packaged task: " << ch::duration_cast(pdur).count() << std::endl; -} - -void factorial_test(thread_pool& tp, unsigned long long number) -{ - std::packaged_task pt(std::bind(mp_factorial, number-1)); - tp.add_task(pt); - auto fu1 = tp.add_task(mp_factorial, number); - auto fu2 = pt.get_future(); - - std::cout << "Results for n & n-1: " << fu1.get() << " " << fu2.get() << std::endl; - std::cout << "With " << tp.get_thread_num() << " threads" << std::endl; - std::cout << "Type of future: " << boost::typeindex::type_id_with_cvr().pretty_name() << std::endl; -} - -void boost_vs_me_test(thread_pool& tp) -{ /* TODO */ } - - -int main(void) -{ - thread_pool tp(4); - - factorial_test(tp, 6); - function_vs_packaged_task_test(tp, 10000); - - - return 0; -}