From 80f56f53d06d847cf9b08d5ef33d4f42f408b1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Mon, 2 May 2016 17:38:08 +0200 Subject: [PATCH] added new test, and refactored old into a method, planned others --- main.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 7ca01d8..214184e 100644 --- a/main.cpp +++ b/main.cpp @@ -48,6 +48,11 @@ /* for the tests */ #include #include +//#include +//#include +//#include + + class thread_pool { @@ -220,27 +225,59 @@ boost::multiprecision::cpp_int mp_factorial(unsigned long long int number) return num; } -int main(void) +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) { - using std::cout; - using std::cin; - using std::endl; namespace ch = std::chrono; namespace mp = boost::multiprecision; - thread_pool tp(4); - auto fu1 = tp.add_task(mp_factorial, 6); - std::packaged_task pt(std::bind(mp_factorial, 5)); + 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(); - cout << fu1.get() << " " << fu2.get() << endl; + 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; +} - cout << tp.get_thread_num() << 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); - cout << "Type of fu1: " << boost::typeindex::type_id_with_cvr().pretty_name() << endl; return 0; }