Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include "monoque.hh"
  2. #include <random>
  3. #include <vector>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <chrono>
  7. #include <deque>
  8. #include <atomic>
  9. #include <thread>
  10.  
  11. int main()
  12. {
  13.   using namespace std;
  14.   using namespace std::chrono;
  15.   using namespace rpnx;
  16.  
  17.   std::ofstream o1("monoque_benchmark.txt");
  18.   //  std::ofstream o1r("monoque::operator[]const.txt");
  19.   std::ofstream o2("vector_benchmark.txt");
  20.   // std::ofstream o2r("vector::operator[]const.txt");
  21.   std::ofstream o3("deque_benchmark.txt");
  22.  
  23.   mt19937_64 r;
  24.  
  25.   size_t round_count = 30000000;
  26.   size_t round_secondary = round_count*4;
  27.  
  28.   /*
  29.   {
  30.     monoque<int> m;
  31.     for (int i = 0; i < round_count; i++)
  32.     {
  33.       m.push_back(i);
  34.     }
  35.   }
  36.  
  37.   {
  38.     vector<int> m;
  39.     for (int i = 0; i < round_count; i++)
  40.     {
  41.       m.push_back(i);
  42.     }
  43.   }
  44.   */
  45.  
  46.   {
  47.     monoque<int> m;
  48.     volatile int tester;
  49.  
  50.     for (size_t i = 0; i < round_count; i++)
  51.     {
  52.       tester = i;      
  53.       m.push_back(int(tester));    
  54.     }
  55.  
  56.     for (size_t i = 0; i < round_count; i++)
  57.       {
  58.         tester = i;
  59.  
  60.         if (m[i] != tester) cout << "ERROR" << endl;
  61.       }
  62.   }
  63.  
  64.   vector<size_t> rds;
  65.  
  66.   for (int i = 0; i < round_count; i++)
  67.     {
  68.       rds.push_back(r() % round_count);
  69.     }
  70.  
  71.   system_clock::time_point start_time;
  72.   system_clock::time_point end_time;
  73.  
  74.   system_clock::time_point start_time_tt;
  75.   system_clock::time_point end_time_tt;
  76.  
  77.   cout  << "For linear structures of size " << round_count << " and random access " << round_secondary << " times, discovered that: " << endl;
  78.  
  79.   {
  80.     vector<int> m;
  81.     start_time = system_clock::now();
  82.  
  83.     volatile int tester;
  84.  
  85.     for (size_t i = 0; i < round_count; i++)
  86.     {
  87.       tester = i;      
  88.       m.push_back(int(tester));    
  89.     }
  90.     end_time = system_clock::now();
  91.     cout << "vector<int> average push_back time: " << (double)(duration_cast<nanoseconds>(end_time-start_time).count())/round_count << " nanoseconds" << endl;
  92.     this_thread::yield();
  93.    
  94.     start_time = system_clock::now();
  95.     for (size_t i = 0; i < round_secondary; i++)
  96.     {    
  97.       tester = m[rds[i % round_count]];
  98.     }
  99.     end_time = system_clock::now();
  100.     cout << "vector<int> average random access time: " << (double)(duration_cast<nanoseconds>(end_time-start_time).count())/round_secondary << " nanoseconds" << endl;
  101.    
  102.   }
  103.  
  104.   {
  105.     monoque<int> m;
  106.     start_time = system_clock::now();
  107.  
  108.     volatile int tester;
  109.  
  110.     for (size_t i = 0; i < round_count; i++)
  111.     {
  112.       tester = i;      
  113.       m.push_back(int(tester));    
  114.     }
  115.     end_time = system_clock::now();
  116.     cout << "monoque<int> average push_back time: " << (double)(duration_cast<nanoseconds>(end_time-start_time).count())/round_count << " nanoseconds" << endl;
  117.     this_thread::yield();
  118.    
  119.     start_time = system_clock::now();
  120.     for (size_t i = 0; i < round_secondary; i++)
  121.     {    
  122.       tester = m[rds[i % round_count]];
  123.     }
  124.     end_time = system_clock::now();
  125.     cout << "monoque<int> average random access time: " << (double)(duration_cast<nanoseconds>(end_time-start_time).count())/round_secondary << " nanoseconds" << endl;
  126.    
  127.   }
  128.  
  129.  
  130.  
  131.  /*
  132.   {
  133.  
  134.  
  135.     vector<int> m;
  136.     for (int  i = 0; i < round_count; i++)
  137.     {
  138.       start_time = system_clock::now();
  139.       std::atomic_signal_fence(memory_order_seq_cst);
  140.       m.push_back(i);
  141.       std::atomic_signal_fence(memory_order_seq_cst);
  142.       end_time = system_clock::now();
  143.       o2 << duration_cast<nanoseconds>(end_time-start_time).count() << endl;
  144.       this_thread::yield();
  145.     }
  146.  
  147.  
  148.     start_time = system_clock::now();
  149.     volatile int tester;
  150.     for (int i = 0; i < round_count; i++)
  151.     {
  152.       std::atomic_signal_fence(memory_order_seq_cst);
  153.       tester = m[rds[i]];      
  154.       std::atomic_signal_fence(memory_order_seq_cst);
  155.     }
  156.     end_time = system_clock::now();
  157.     //duration_cast<nanoseconds>(end_time-start_time).count())/round_count << endl;
  158.  
  159.   }
  160.  */
  161.  
  162.  
  163.  
  164.   {
  165.    
  166.     deque<int> m;
  167.     volatile int tester;
  168.  
  169.     start_time = system_clock::now();
  170.     for (size_t  i = 0; i < round_count; i++)
  171.     {
  172.       tester = i;
  173.       m.push_back(int(tester));
  174.     }
  175.     end_time = system_clock::now();
  176.     cout << "deque<int> average push_back time: " << double(duration_cast<nanoseconds>(end_time-start_time).count())/round_count <<  " nanoseconds" << endl;
  177.     this_thread::yield();
  178.  
  179.     start_time = system_clock::now();
  180.    
  181.     for (size_t i = 0; i < round_secondary; i++)
  182.     {
  183.       tester = m[rds[i % round_count]];
  184.     }
  185.  
  186.     end_time = system_clock::now();
  187.     cout << "deque<int> average random access time: " << (double)(duration_cast<nanoseconds>(end_time-start_time).count())/round_secondary << " nanoseconds" << endl;
  188.  
  189.   }
  190.  
  191.   return 0;
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement