thread-pool/test.cpp

78 lines
2.3 KiB
C++
Raw Normal View History

#include "thread_pool.hpp"
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/type_index.hpp>
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// 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<void()>(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<ch::microseconds>(fdur).count() <<
std::endl << "Packaged task: " << ch::duration_cast<ch::microseconds>(pdur).count() << std::endl;
}
void factorial_test(thread_pool& tp, unsigned long long number)
{
std::packaged_task<boost::multiprecision::cpp_int()> 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<decltype(fu1)>().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;
}