Advertisement
sp2danny

testcode

Oct 24th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | None | 0 0
  1. // ---------------------------------------------------------------------------------------------
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <list>
  6. #include <initializer_list>
  7. #include <sstream>
  8. #include <cassert>
  9. #include <cstring>
  10. #include <set>
  11. #include <algorithm>
  12. #include <random>
  13. #include <chrono>
  14. #include <limits>
  15. #include <array>
  16. #include <fstream>
  17.  
  18. #include "../avl/avl-array-C++-1.2.1/src/avl_array.hpp"
  19.  
  20. template<typename T>
  21. struct MyAlloc
  22. {
  23.     typedef T value_type;
  24.  
  25.     MyAlloc() = default;
  26.     template<typename U> MyAlloc(const MyAlloc<U>&) {}
  27.  
  28.     T* allocate(int n)
  29.     {
  30.         T* p = buffer+cnt;
  31.         cnt += n;
  32.         return p;
  33.     }
  34.     void deallocate(T*, int n)
  35.     {
  36.         dcnt+=n;
  37.         if(cnt==dcnt)
  38.             cnt=dcnt=0;
  39.     }
  40. private:
  41.     static T buffer[64];
  42.     static int cnt,dcnt;
  43. };
  44.  
  45. template<typename T>
  46. T MyAlloc<T>::buffer[64];
  47.  
  48. template<typename T>
  49. int MyAlloc<T>::cnt = 0;
  50.  
  51. template<typename T>
  52. int MyAlloc<T>::dcnt = 0;
  53.  
  54. typedef unsigned short T;
  55.  
  56. namespace detail
  57. {
  58.  
  59.     const int DIST = 20000;
  60.     const int AVGSZ = 23;
  61.     const int NTH = 11;
  62.  
  63.     std::default_random_engine generator( (unsigned int)std::chrono::system_clock::now().time_since_epoch().count() );
  64.     std::uniform_int_distribution<T> distribution(0,DIST);
  65.  
  66.     T makeone()
  67.     {
  68.         return distribution(generator);
  69.     }
  70.  
  71.     T nth_element( const std::multiset<T>& mst, int n )
  72.     {
  73.         auto iter = mst.begin();
  74.         std::advance(iter,n);
  75.         return *iter;
  76.     }
  77.  
  78.     T alt_0( int reps )
  79.     {
  80.         T dummy = 0;
  81.         for( int r=0; r<reps; ++r )
  82.         {
  83.             dummy += makeone();
  84.         }
  85.         return dummy;
  86.     }
  87.  
  88.     T alt_1( int reps )
  89.     {
  90.         T dummy = 0;
  91.         for( int r=0; r<reps; ++r )
  92.         {
  93.             std::vector<T> vt;
  94.             vt.reserve(AVGSZ);
  95.             for( int i=0; i<AVGSZ; ++i )
  96.                 vt.push_back( makeone() );
  97.             nth_element( vt.begin(), vt.begin()+NTH, vt.end() );
  98.             dummy += vt[NTH];
  99.         }
  100.         return dummy;
  101.     }
  102.  
  103.     T alt_2( int reps )
  104.     {
  105.         T dummy = 0;
  106.         for( int r=0; r<reps; ++r )
  107.         {
  108.             std::multiset<T> mst;
  109.             for( int i=0; i<AVGSZ; ++i )
  110.                 mst.insert( makeone() );
  111.             dummy += nth_element( mst, NTH );
  112.         }
  113.         return dummy;
  114.     }
  115.  
  116.     T alt_2b( int reps )
  117.     {
  118.         T dummy = 0;
  119.         for( int r=0; r<reps; ++r )
  120.         {
  121.             std::set<T> st;
  122.             for( int i=0; i<AVGSZ; ++i )
  123.                 st.insert( makeone() );
  124.             auto iter = st.begin();
  125.             std::advance(iter,NTH );
  126.             dummy += *iter;
  127.         }
  128.         return dummy;
  129.     }
  130.  
  131.     T alt_3( int reps )
  132.     {
  133.         T dummy = 0;
  134.         for( int r=0; r<reps; ++r )
  135.         {
  136.             std::list<T> lt;
  137.             for( int i=0; i<AVGSZ; ++i )
  138.             {
  139.                 auto val = makeone();
  140.                 auto iter = std::lower_bound( lt.begin(), lt.end(), val );
  141.                 lt.insert( iter, val );
  142.                 if( lt.size() > (NTH+1) )
  143.                     lt.pop_back();
  144.             }
  145.             dummy += lt.back();
  146.         }
  147.         return dummy;
  148.     }
  149.  
  150.     T alt_3b( int reps )
  151.     {
  152.         T dummy = 0;
  153.         for( int r=0; r<reps; ++r )
  154.         {
  155.             std::list<T,MyAlloc<int>> lt;
  156.             for( int i=0; i<AVGSZ; ++i )
  157.             {
  158.                 auto val = makeone();
  159.                 auto iter = std::lower_bound( lt.begin(), lt.end(), val );
  160.                 lt.insert( iter, val );
  161.                 if( lt.size() > (NTH+1) )
  162.                     lt.pop_back();
  163.             }
  164.             dummy += lt.back();
  165.         }
  166.         return dummy;
  167.     }
  168.  
  169.     T alt_4( int reps )
  170.     {
  171.         T dummy = 0;
  172.         for( int r=0; r<reps; ++r )
  173.         {
  174.             mkr::avl_array<T> aat;
  175.             for( int i=0; i<AVGSZ; ++i )
  176.             {
  177.                 aat.insert_sorted(makeone());
  178.             }
  179.             dummy += aat[NTH];
  180.         }
  181.         return dummy;
  182.     }
  183.  
  184.     T alt_5( int reps )
  185.     {
  186.         T dummy = 0;
  187.         for( int r=0; r<reps; ++r )
  188.         {
  189.             mkr::avl_array<T> aat;
  190.             for( int i=0; i<AVGSZ; ++i )
  191.             {
  192.                 aat.insert_sorted(makeone());
  193.                 if( aat.size() > (NTH+1) )
  194.                     aat.pop_back();
  195.             }
  196.             dummy += aat[NTH];
  197.         }
  198.         return dummy;
  199.     }
  200.  
  201.     T alt_6( int reps )
  202.     {
  203.         T dummy = 0;
  204.         for( int r=0; r<reps; ++r )
  205.         {
  206.             unsigned char hist[DIST+2] = {0};
  207.             for( int i=0; i<AVGSZ; ++i )
  208.             {
  209.                 T val = makeone();
  210.                 hist[val] += 1;
  211.             }
  212.             size_t idx=0;
  213.             size_t rem=NTH;
  214.             T nth;
  215.             while(true)
  216.             {
  217.                 if( hist[idx] > rem )
  218.                 {
  219.                     nth = idx;
  220.                     break;
  221.                 }
  222.                 rem -= hist[idx];
  223.                 ++idx;
  224.             }
  225.             dummy += nth;
  226.         }
  227.         return dummy;
  228.     }
  229.  
  230.     template<typename T>
  231.     auto timeit( T f, int reps ) -> auto
  232.     {
  233.         using clk = std::chrono::high_resolution_clock;
  234.         auto t1 = clk::now();
  235.         f(reps);
  236.         auto t2 = clk::now();
  237.         return std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
  238.     }
  239.  
  240. }
  241.  
  242. int main()
  243. {
  244.  
  245.     using namespace std;
  246.     using namespace detail;
  247.  
  248.     #ifndef NDEBUG
  249.     const int REPS = 5000;
  250.     #else
  251.     const int REPS = 100000;
  252.     #endif
  253.  
  254.     ofstream out("result.txt");
  255.  
  256.     out << "testing "<< REPS << " repetitions" << endl;
  257.  
  258.     out << "variant 0, no-op (for overhead measure)" << endl;
  259.     out << timeit( alt_0, REPS ) << " ms" << endl;
  260.  
  261.     out << "variant 1, vector + nth_element" << endl;
  262.     out << timeit( alt_1, REPS ) << " ms" << endl;
  263.  
  264.     out << "variant 2, multiset + advance" << endl;
  265.     out << timeit( alt_2, REPS ) << " ms" << endl;
  266.  
  267.     out << "variant 2b, set (not fully conformant)" << endl;
  268.     out << timeit( alt_2b, REPS ) << " ms" << endl;
  269.  
  270.     out << "variant 3, list + lower_bound" << endl;
  271.     out << timeit( alt_3, REPS ) << " ms" << endl;
  272.  
  273.     out << "variant 3b, list + block-allocator" << endl;
  274.     out << timeit( alt_3b, REPS ) << " ms" << endl;
  275.  
  276.     out << "variant 4, avl-tree + insert_sorted" << endl;
  277.     out << timeit( alt_4, REPS ) << " ms" << endl;
  278.  
  279.     out << "variant 4b, avl-tree + prune" << endl;
  280.     out << timeit( alt_5, REPS ) << " ms" << endl;
  281.  
  282.     out << "variant 5, histogram" << endl;
  283.     out << timeit( alt_6, REPS ) << " ms" << endl;
  284.  
  285.     cout << endl << "done" << endl;
  286.  
  287.     // fgetc(stdin);
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement