Advertisement
FatalCatharsis

New Concurrency Tests

Jun 24th, 2014
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <thread>
  4. #include <mutex>
  5. #include <functional>
  6. #include <cstdlib>
  7. #include <chrono>
  8. #include <ctime>
  9. #include <boost/asio/io_service.hpp>
  10. #include <boost/thread.hpp>
  11. #include <boost/bind.hpp>
  12.  
  13. int main()
  14. {
  15.     const int NUM_ACCESSES = 400000;
  16.     const int NUM_ELEMENTS = 100;
  17.     const int NUM_THREADS = 4;
  18.     const int NUM_RUNS = 25;
  19.  
  20.     srand(time(NULL));
  21.     std::vector<int> test(NUM_ELEMENTS, 0);
  22.     std::mutex test_mutexes[NUM_ELEMENTS];
  23.     std::mutex share;
  24.     int average = 0;
  25.  
  26.     std::function<void()> func1 = [&]()
  27.     {
  28.         int index;
  29.         for(unsigned int i = 0; i < NUM_ACCESSES/NUM_THREADS; i++)
  30.         {
  31.             index = rand() % NUM_ELEMENTS;
  32.             test_mutexes[index].lock();
  33.             test[index] = rand();
  34.             test_mutexes[index].unlock();
  35.         }
  36.     };
  37.  
  38.     std::function<void()> func2 = [&]()
  39.     {
  40.         int index;
  41.         for(unsigned int i = 0; i < NUM_ACCESSES/NUM_THREADS; i++)
  42.         {
  43.             index = rand() % NUM_ELEMENTS;
  44.             share.lock();
  45.             test[index] = rand();
  46.             share.unlock();
  47.         }
  48.     };
  49.  
  50.     std::cout << "Non-Concurrent Control Test:\n";
  51.     for(int i = 0; i < NUM_RUNS; i++)
  52.     {
  53.         std::chrono::time_point<std::chrono::steady_clock> start, end;
  54.         start = std::chrono::steady_clock::now();
  55.         for(int i = 0; i < NUM_ACCESSES; i++)
  56.         {
  57.             int index = rand() % NUM_ELEMENTS;
  58.             test[index] = rand();
  59.         }
  60.         end = std::chrono::steady_clock::now();
  61.         auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  62.         std::cout << "Elapsed time: " << elapse.count() << "ms\n";
  63.         average += elapse.count();
  64.     }
  65.     std::cout << "Average time: " << average/NUM_RUNS << "ms\n\n";
  66.  
  67.     std::cout << "Individual Mutex Implement Test:\n";
  68.     average = 0;
  69.     for(int i = 0; i < NUM_RUNS; i++)
  70.     {
  71.         std::chrono::time_point<std::chrono::steady_clock> start, end;
  72.         boost::thread threads[NUM_THREADS] = boost::thread(func1);
  73.         start = std::chrono::steady_clock::now();
  74.  
  75.         for(auto& thread: threads )
  76.             thread.join();
  77.  
  78.         end = std::chrono::steady_clock::now();
  79.         auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  80.         std::cout << "Elapsed time: " << elapse.count() << "ms\n";
  81.         average += elapse.count();
  82.     }
  83.     std::cout << "Average time: " << average/NUM_RUNS << "ms\n";
  84.  
  85.     average = 0;
  86.     std::cout << "\n";
  87.     std::cout << "Shared Mutex Implement Test:\n";
  88.     for(int i = 0; i < NUM_RUNS; i++)
  89.     {
  90.         std::chrono::time_point<std::chrono::steady_clock> start, end;
  91.         boost::thread threads[NUM_THREADS] = boost::thread(func2);
  92.         start = std::chrono::steady_clock::now();
  93.  
  94.         for(auto& thread: threads )
  95.             thread.join();
  96.  
  97.         end = std::chrono::steady_clock::now();
  98.         auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  99.         std::cout << "Elapsed time: " << elapse.count() << "ms\n";
  100.         average += elapse.count();
  101.     }
  102.     std::cout << "Average time: " << average/NUM_RUNS << "ms\n\n";
  103.  
  104.     std::cout << "boost threadpool test on individual mutexes\n";
  105.     average = 0;
  106.     boost::asio::io_service ioService;
  107.     boost::thread_group threadpool;
  108.  
  109.     std::function<void()> func3 = [&]()
  110.     {
  111.         int index = rand() % NUM_ELEMENTS;
  112.         test_mutexes[index].lock();
  113.         test[index] = rand();
  114.         test_mutexes[index].unlock();
  115.     };
  116.  
  117.     boost::asio::io_service::work work(ioService);
  118.  
  119.     for(int i = 0; i < 4; i++)
  120.         boost::bind(&boost::asio::io_service::run, &ioService);
  121.  
  122.     for(int i = 0; i < NUM_RUNS; i++)
  123.     {
  124.         std::chrono::time_point<std::chrono::steady_clock> start, end;
  125.         start = std::chrono::steady_clock::now();
  126.         for(int i = 0; i < NUM_ACCESSES; i++)
  127.         {
  128.             ioService.post(boost::bind(func3));
  129.         }
  130.  
  131.         threadpool.join_all();
  132.         end = std::chrono::steady_clock::now();
  133.         auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  134.         std::cout << "Elapsed time: " << elapse.count() << "ms\n";
  135.         average += elapse.count();
  136.     }
  137.     std::cout << "Average time: " << average/NUM_RUNS << "ms\n\n";
  138.  
  139.     std::cout << "boost threadpool test on a shared mutex\n";
  140.     average = 0;
  141.  
  142.     std::function <void()> func4 =[&]()
  143.     {
  144.         int index = rand() & NUM_ELEMENTS;
  145.         share.lock();
  146.         test[index] = rand();
  147.         share.unlock();
  148.     };
  149.  
  150.     for(int i = 0; i < NUM_RUNS; i++)
  151.     {
  152.         std::chrono::time_point<std::chrono::steady_clock> start, end;
  153.         start = std::chrono::steady_clock::now();
  154.  
  155.         for(int i = 0; i < NUM_ACCESSES; i++)
  156.             ioService.post(boost::bind(func4));
  157.  
  158.         threadpool.join_all();
  159.         end = std::chrono::steady_clock::now();
  160.         auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  161.         std::cout << "Elapsed time: " << elapse.count() << "ms\n";
  162.         average += elapse.count();
  163.     }
  164.     std::cout << "Average time: " << average/NUM_RUNS << "ms\n\n";
  165.     ioService.stop();
  166.  
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement