Guest User

CEV vs LUT vs BEV valgrind ready

a guest
Dec 29th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <random>
  4. #include <functional>
  5. #include <vector>
  6. #include <unordered_map>
  7. #include <algorithm>
  8. #include <array>
  9. #include <utility>
  10. #include <unistd.h>
  11.  
  12. namespace chro = std::chrono;
  13.  
  14. #ifndef S
  15.     #define S 2
  16. #endif
  17.  
  18. #ifndef STR_SIZE
  19.     #define STR_SIZE 2048
  20. #endif
  21.  
  22. #ifndef SET_SIZE
  23.     #define SET_SIZE 10000
  24. #endif
  25.  
  26. #ifndef UNITS
  27.     #define UNITS microseconds
  28. #endif
  29.  
  30. #ifndef UTYPE
  31.     #define UTYPE double
  32. #endif
  33.  
  34. typedef UTYPE t_units;
  35. typedef chro::UNITS t_timeunits;
  36.  
  37. template<typename T> std::pair<double,double> stats( T b, T e)
  38. {
  39.  
  40.     typedef typename T::value_type t_val ;
  41.  
  42.     t_val total = std::accumulate( b, e, 0);
  43.  
  44.   double s(e-b);
  45.     double mu{double(total)/s};
  46.     double sig{0};
  47.     std::for_each(b,e,[&sig,&mu](t_val const & e){ sig+= ( double(e)-mu )*(double(e)-mu);
  48.             });
  49.  
  50.     double err = sqrt( sig/( s-1) );
  51.  
  52.  
  53.     return std::make_pair(mu,err);
  54. }
  55.  
  56. int decfromhex1(int const x)
  57. {
  58.     return x<58?x-48:x-87;
  59. }
  60.  
  61. int decfromhex2(int const x)
  62. {
  63.     return x & 64 ? x^87 : x^48 ;
  64. }
  65.  
  66.  
  67. int decfromhex3(int const ix)
  68. {
  69.     static int constexpr x[255]={
  70.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  71.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  72.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  73.         1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,
  74.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  75.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  76.         0,10,11,12,13,14,15,0,0,0,0,0,0,
  77.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  78.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  79.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  80.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  81.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  82.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  83.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  84.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  85.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  86.     };
  87.  
  88.     return x[ix];
  89. }
  90.  
  91. #define BENCHMARK(benchfun, start , fin,timings) \
  92.     std::vector<t_units> timings; timings.reserve(MAXN*MAXCH);\
  93.     sleep(S);\
  94.     auto start = chro::high_resolution_clock::now();\
  95.     for(int k = 0; k!=MAXN; ++k)\
  96.     {\
  97.         make_local_copy(k);\
  98.         auto tic = chro::high_resolution_clock::now();\
  99.         for(int i = 0; i!=MAXCH;i++)\
  100.         {\
  101.             ints[i]=benchfun(chars[i]);\
  102.         }\
  103.         auto toc = chro::high_resolution_clock::now();\
  104.         timings.push_back(chro::duration_cast<t_timeunits>(chro::duration<t_units>(toc-tic)).count());\
  105.         append_results();\
  106.     }\
  107.     auto fin = chro::high_resolution_clock::now();\
  108.  
  109.  
  110. #define HEATUP(heatfun)\
  111.     sleep(S);\
  112.     for(int k = 0; k!=MAXN; ++k)\
  113.     {\
  114.         make_local_copy(k);\
  115.         for(int i = 0; i!=MAXCH;i++)\
  116.         {\
  117.             ints[i]=heatfun(chars[i]);\
  118.         }\
  119.         append_results();\
  120.     }\
  121.  
  122.  
  123.  
  124. int main()
  125. {
  126.  
  127.     auto rand1 = std::bind(std::uniform_int_distribution<>(48,57), std::mt19937(std::random_device()()));
  128.     auto rand2 = std::bind(std::uniform_int_distribution<>(97,102), std::mt19937(std::random_device()()));
  129.     auto rand = std::bind(std::uniform_int_distribution<>(0,2), std::mt19937(std::random_device()()));
  130.  
  131.     int const MAXCH = STR_SIZE;
  132.     int const MAXN = SET_SIZE;
  133.  
  134.     char chars[MAXCH];
  135.     int ints[MAXCH];
  136.  
  137.     std::array<std::array<char,MAXCH>, MAXN> vin;
  138.  
  139.     std::vector<int> vout;
  140.         vout.reserve(6*MAXCH*MAXN);
  141.  
  142.     auto fillv = [&](){
  143.         std::array<char,MAXCH> chars;
  144.         for(int i = 0; i!=MAXCH; i++)
  145.         {
  146.             chars[i] = rand() ? rand1() : rand2(); // higher hex happen at 1/3 prob
  147.         }
  148.         return chars;
  149.     };
  150.  
  151.  
  152.     auto make_local_copy = [&chars,&vin](int const k)
  153.     {
  154.         std::copy(vin[k].begin(),vin[k].end(),&chars[0]);
  155.     };
  156.  
  157.     auto append_results = [&ints, &vout]()
  158.     {
  159.         for( auto const & r : ints ) vout.push_back(r);
  160.     };
  161.  
  162.     std::generate_n(vin.begin(),MAXN,fillv);
  163.  
  164. #ifndef NOCEV
  165.     HEATUP(decfromhex1);
  166.     BENCHMARK(decfromhex1,t1_0,t1_1,timings1);
  167. #endif
  168.  
  169. #ifndef NOBEV
  170.     HEATUP(decfromhex2);
  171.     BENCHMARK(decfromhex2,t2_0,t2_1,timings2);
  172. #endif
  173.  
  174. #ifndef NOLUT
  175.   HEATUP(decfromhex3);
  176.     BENCHMARK(decfromhex3,t3_0,t3_1,timings3);
  177. #endif
  178.  
  179.   auto dump_input = [&]()
  180.     {
  181.     for(int k = 0 ; k != MAXN ; ++k)
  182.     {
  183.         for(int i = 0 ; i!=MAXCH; ++i)
  184.         {
  185.             std::cout << vin[k][i] ;
  186.         }
  187.         std::cout << std::endl;
  188.     }
  189.     std::cout << std::endl;
  190.     };
  191.  
  192.     auto dump_results = [&]()
  193.     {
  194.     for(auto const & r : vout) { std::cout << r <<" " ; } std::cout << std::endl;
  195.     };
  196.  
  197.     int m(0);
  198.     for(auto const & r : vout)
  199.     {
  200.     r < 10? --m: ++m;
  201.     }
  202.  
  203. #ifdef SHOW_INPUT
  204.     dump_input();
  205. #endif
  206.  
  207. #ifdef SHOW_RESULTS
  208.     dump_results();
  209. #endif
  210.  
  211. #ifndef NOCEV
  212.     std::pair<double,double> r1 = stats(timings1.begin(),timings1.end());
  213. #endif
  214.  
  215. #ifndef NOBEV
  216.     std::pair<double,double> r2 = stats(timings2.begin(),timings2.end());
  217. #endif
  218.  
  219. #ifndef NOLUT
  220.     std::pair<double,double> r3 = stats(timings3.begin(),timings3.end());
  221. #endif
  222.  
  223. #define PSTR_(x) #x
  224. #define PSTR(x) PSTR_(x)
  225.  
  226.   std:: cout << "sign: " << m/6 << std::endl;
  227.     std::cout << "-------------------------------------------------------------------" << std::endl;
  228. #ifndef NOCEV
  229.     std::cout << "(CEV) Total: " << chro::duration_cast<t_timeunits>(chro::duration<t_units>(t1_1-t1_0)).count() << " " << PSTR(UNITS) << " - ";
  230.     std::cout << "mean: " << r1.first << " " << PSTR(UNITS) << "\terror: " << r1.second << " " << PSTR(UNITS) << std::endl;
  231. #endif
  232.  
  233. #ifndef NOBEV
  234.     std::cout << "(BEV) Total: " << chro::duration_cast<t_timeunits>(chro::duration<t_units>(t2_1-t2_0)).count() << " " << PSTR(UNITS) << " - ";
  235.     std::cout << "mean: " << r2.first << " " << PSTR(UNITS) << "\terror: " << r2.second << " " << PSTR(UNITS) << std::endl;
  236. #endif
  237.  
  238. #ifndef NOLUT
  239.     std::cout << "(LUT) Total: " << chro::duration_cast<t_timeunits>(chro::duration<t_units>(t3_1-t3_0)).count() << " " << PSTR(UNITS) << " - ";
  240.     std::cout << "mean: " << r3.first << " " << PSTR(UNITS) << "\terror: " << r3.second << " " << PSTR(UNITS) << std::endl;
  241. #endif
  242.  
  243.     std::cout << "-------------------------------------------------------------------" << std::endl;
  244.     std::cout << "Precision: " << chro::high_resolution_clock::rep(1) << " ns" << std::endl;
  245.  
  246.     return 0;
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment