Advertisement
Guest User

CEV vs LUT vs BEV valgrind ready

a guest
Jan 5th, 2016
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 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[128]={
  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.     };
  79.  
  80.     return x[ix];
  81. }
  82.  
  83. #define BENCHMARK(benchfun, start , fin,timings) \
  84.     \
  85.     std::vector<t_units> timings; timings.reserve(MAXN*MAXCH);\
  86.   \
  87.     auto start = chro::high_resolution_clock::now();\
  88.   \
  89.     for(int k = 0; k!=MAXN; ++k)\
  90.   \
  91.     {\
  92.     \
  93.         make_local_copy(k);\
  94.         \
  95.         auto tic = chro::high_resolution_clock::now();\
  96.         \
  97.         for(int i = 0; i!=MAXCH;i++)\
  98.         \
  99.         {\
  100.             \
  101.             ints[i]=benchfun(chars[i]);\
  102.             \
  103.         }\
  104.         \
  105.         auto toc = chro::high_resolution_clock::now();\
  106.         \
  107.         timings.push_back(chro::duration_cast<t_timeunits>(chro::duration<t_units>(toc-tic)).count());\
  108.         \
  109.         append_results();\
  110.         \
  111.     }\
  112. \
  113.     auto fin = chro::high_resolution_clock::now();\
  114. \
  115.  
  116.  
  117. #define HEATUP(heatfun)\
  118.     sleep(S);\
  119.     for(int k = 0; k!=MAXN; ++k)\
  120.     {\
  121.         make_local_copy(k);\
  122.         for(int i = 0; i!=MAXCH;i++)\
  123.         {\
  124.             ints[i]=heatfun();\
  125.         }\
  126.         append_results();\
  127.     }\
  128.  
  129.  
  130.  
  131. int main()
  132. {
  133.  
  134.     auto rand1 = std::bind(std::uniform_int_distribution<>(48,57), std::mt19937(std::random_device()()));
  135.     auto rand2 = std::bind(std::uniform_int_distribution<>(97,102), std::mt19937(std::random_device()()));
  136.     auto rand = std::bind(std::uniform_int_distribution<>(0,2), std::mt19937(std::random_device()()));
  137.  
  138.     int const MAXCH = STR_SIZE;
  139.     int const MAXN = SET_SIZE;
  140.  
  141.     char chars[MAXCH];
  142.     int ints[MAXCH];
  143.  
  144.     std::array<std::array<char,MAXCH>, MAXN> vin;
  145.  
  146.     std::vector<int> vout;
  147.         vout.reserve(6*MAXCH*MAXN);
  148.  
  149.     auto fillv = [&](){
  150.         std::array<char,MAXCH> chars;
  151.         for(int i = 0; i!=MAXCH; i++)
  152.         {
  153.             chars[i] = rand() ? rand1() : rand2(); // higher hex happen at 1/3 prob
  154.         }
  155.         return chars;
  156.     };
  157.  
  158.  
  159.     auto make_local_copy = [&chars,&vin](int const k)
  160.     {
  161.         std::copy(vin[k].begin(),vin[k].end(),&chars[0]);
  162.     };
  163.  
  164.     auto append_results = [&ints, &vout]()
  165.     {
  166.         for( auto const & r : ints ) vout.push_back(r);
  167.     };
  168.  
  169.     std::generate_n(vin.begin(),MAXN,fillv);
  170.  
  171. #ifndef NOCEV
  172.     HEATUP(rand2);
  173.     BENCHMARK(decfromhex1,t1_0,t1_1,timings1);
  174. #endif
  175.  
  176. #ifndef NOBEV
  177.     HEATUP(rand2);
  178.     BENCHMARK(decfromhex2,t2_0,t2_1,timings2);
  179. #endif
  180.  
  181. #ifndef NOLUT
  182.   HEATUP(rand2);
  183.     BENCHMARK(decfromhex3,t3_0,t3_1,timings3);
  184. #endif
  185.  
  186.   auto dump_input = [&]()
  187.     {
  188.     for(int k = 0 ; k != MAXN ; ++k)
  189.     {
  190.         for(int i = 0 ; i!=MAXCH; ++i)
  191.         {
  192.             std::cout << vin[k][i] ;
  193.         }
  194.         std::cout << std::endl;
  195.     }
  196.     std::cout << std::endl;
  197.     };
  198.  
  199.     auto dump_results = [&]()
  200.     {
  201.     for(auto const & r : vout) { std::cout << r <<" " ; } std::cout << std::endl;
  202.     };
  203.  
  204.     int m(0);
  205.     for(auto const & r : vout)
  206.     {
  207.     r < 10? --m: ++m;
  208.     }
  209.  
  210. #ifdef SHOW_INPUT
  211.     dump_input();
  212. #endif
  213.  
  214. #ifdef SHOW_RESULTS
  215.     dump_results();
  216. #endif
  217.  
  218. #ifndef NOCEV
  219.     std::pair<double,double> r1 = stats(timings1.begin(),timings1.end());
  220. #endif
  221.  
  222. #ifndef NOBEV
  223.     std::pair<double,double> r2 = stats(timings2.begin(),timings2.end());
  224. #endif
  225.  
  226. #ifndef NOLUT
  227.     std::pair<double,double> r3 = stats(timings3.begin(),timings3.end());
  228. #endif
  229.  
  230. #define PSTR_(x) #x
  231. #define PSTR(x) PSTR_(x)
  232.  
  233.   std:: cout << "sign: " << m/6 << std::endl;
  234.     std::cout << "-------------------------------------------------------------------" << std::endl;
  235. #ifndef NOCEV
  236.     std::cout << "(CEV) Total: " << chro::duration_cast<t_timeunits>(chro::duration<t_units>(t1_1-t1_0)).count() << " " << PSTR(UNITS) << " - ";
  237.     std::cout << "mean: " << r1.first << " " << PSTR(UNITS) << "\terror: " << r1.second << " " << PSTR(UNITS) << std::endl;
  238. #endif
  239.  
  240. #ifndef NOBEV
  241.     std::cout << "(BEV) Total: " << chro::duration_cast<t_timeunits>(chro::duration<t_units>(t2_1-t2_0)).count() << " " << PSTR(UNITS) << " - ";
  242.     std::cout << "mean: " << r2.first << " " << PSTR(UNITS) << "\terror: " << r2.second << " " << PSTR(UNITS) << std::endl;
  243. #endif
  244.  
  245. #ifndef NOLUT
  246.     std::cout << "(LUT) Total: " << chro::duration_cast<t_timeunits>(chro::duration<t_units>(t3_1-t3_0)).count() << " " << PSTR(UNITS) << " - ";
  247.     std::cout << "mean: " << r3.first << " " << PSTR(UNITS) << "\terror: " << r3.second << " " << PSTR(UNITS) << std::endl;
  248. #endif
  249.  
  250.     std::cout << "-------------------------------------------------------------------" << std::endl;
  251.     std::cout << "Precision: " << chro::high_resolution_clock::rep(1) << " ns" << std::endl;
  252.  
  253.     return 0;
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement