Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- #include <random>
- #include <functional>
- #include <vector>
- #include <unordered_map>
- #include <algorithm>
- #include <array>
- #include <utility>
- #include <unistd.h>
- namespace chro = std::chrono;
- #ifndef S
- #define S 2
- #endif
- #ifndef STR_SIZE
- #define STR_SIZE 2048
- #endif
- #ifndef SET_SIZE
- #define SET_SIZE 10000
- #endif
- #ifndef UNITS
- #define UNITS microseconds
- #endif
- #ifndef UTYPE
- #define UTYPE double
- #endif
- typedef UTYPE t_units;
- typedef chro::UNITS t_timeunits;
- template<typename T> std::pair<double,double> stats( T b, T e)
- {
- typedef typename T::value_type t_val ;
- t_val total = std::accumulate( b, e, 0);
- double s(e-b);
- double mu{double(total)/s};
- double sig{0};
- std::for_each(b,e,[&sig,&mu](t_val const & e){ sig+= ( double(e)-mu )*(double(e)-mu);
- });
- double err = sqrt( sig/( s-1) );
- return std::make_pair(mu,err);
- }
- int decfromhex1(int const x)
- {
- return x<58?x-48:x-87;
- }
- int decfromhex2(int const x)
- {
- return x & 64 ? x^87 : x^48 ;
- }
- int decfromhex3(int const ix)
- {
- static int constexpr x[128]={
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,10,11,12,13,14,15,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- };
- return x[ix];
- }
- #define BENCHMARK(benchfun, start , fin,timings) \
- \
- std::vector<t_units> timings; timings.reserve(MAXN*MAXCH);\
- \
- auto start = chro::high_resolution_clock::now();\
- \
- for(int k = 0; k!=MAXN; ++k)\
- \
- {\
- \
- make_local_copy(k);\
- \
- auto tic = chro::high_resolution_clock::now();\
- \
- for(int i = 0; i!=MAXCH;i++)\
- \
- {\
- \
- ints[i]=benchfun(chars[i]);\
- \
- }\
- \
- auto toc = chro::high_resolution_clock::now();\
- \
- timings.push_back(chro::duration_cast<t_timeunits>(chro::duration<t_units>(toc-tic)).count());\
- \
- append_results();\
- \
- }\
- \
- auto fin = chro::high_resolution_clock::now();\
- \
- #define HEATUP(heatfun)\
- sleep(S);\
- for(int k = 0; k!=MAXN; ++k)\
- {\
- make_local_copy(k);\
- for(int i = 0; i!=MAXCH;i++)\
- {\
- ints[i]=heatfun();\
- }\
- append_results();\
- }\
- int main()
- {
- auto rand1 = std::bind(std::uniform_int_distribution<>(48,57), std::mt19937(std::random_device()()));
- auto rand2 = std::bind(std::uniform_int_distribution<>(97,102), std::mt19937(std::random_device()()));
- auto rand = std::bind(std::uniform_int_distribution<>(0,2), std::mt19937(std::random_device()()));
- int const MAXCH = STR_SIZE;
- int const MAXN = SET_SIZE;
- char chars[MAXCH];
- int ints[MAXCH];
- std::array<std::array<char,MAXCH>, MAXN> vin;
- std::vector<int> vout;
- vout.reserve(6*MAXCH*MAXN);
- auto fillv = [&](){
- std::array<char,MAXCH> chars;
- for(int i = 0; i!=MAXCH; i++)
- {
- chars[i] = rand() ? rand1() : rand2(); // higher hex happen at 1/3 prob
- }
- return chars;
- };
- auto make_local_copy = [&chars,&vin](int const k)
- {
- std::copy(vin[k].begin(),vin[k].end(),&chars[0]);
- };
- auto append_results = [&ints, &vout]()
- {
- for( auto const & r : ints ) vout.push_back(r);
- };
- std::generate_n(vin.begin(),MAXN,fillv);
- #ifndef NOCEV
- HEATUP(rand2);
- BENCHMARK(decfromhex1,t1_0,t1_1,timings1);
- #endif
- #ifndef NOBEV
- HEATUP(rand2);
- BENCHMARK(decfromhex2,t2_0,t2_1,timings2);
- #endif
- #ifndef NOLUT
- HEATUP(rand2);
- BENCHMARK(decfromhex3,t3_0,t3_1,timings3);
- #endif
- auto dump_input = [&]()
- {
- for(int k = 0 ; k != MAXN ; ++k)
- {
- for(int i = 0 ; i!=MAXCH; ++i)
- {
- std::cout << vin[k][i] ;
- }
- std::cout << std::endl;
- }
- std::cout << std::endl;
- };
- auto dump_results = [&]()
- {
- for(auto const & r : vout) { std::cout << r <<" " ; } std::cout << std::endl;
- };
- int m(0);
- for(auto const & r : vout)
- {
- r < 10? --m: ++m;
- }
- #ifdef SHOW_INPUT
- dump_input();
- #endif
- #ifdef SHOW_RESULTS
- dump_results();
- #endif
- #ifndef NOCEV
- std::pair<double,double> r1 = stats(timings1.begin(),timings1.end());
- #endif
- #ifndef NOBEV
- std::pair<double,double> r2 = stats(timings2.begin(),timings2.end());
- #endif
- #ifndef NOLUT
- std::pair<double,double> r3 = stats(timings3.begin(),timings3.end());
- #endif
- #define PSTR_(x) #x
- #define PSTR(x) PSTR_(x)
- std:: cout << "sign: " << m/6 << std::endl;
- std::cout << "-------------------------------------------------------------------" << std::endl;
- #ifndef NOCEV
- std::cout << "(CEV) Total: " << chro::duration_cast<t_timeunits>(chro::duration<t_units>(t1_1-t1_0)).count() << " " << PSTR(UNITS) << " - ";
- std::cout << "mean: " << r1.first << " " << PSTR(UNITS) << "\terror: " << r1.second << " " << PSTR(UNITS) << std::endl;
- #endif
- #ifndef NOBEV
- std::cout << "(BEV) Total: " << chro::duration_cast<t_timeunits>(chro::duration<t_units>(t2_1-t2_0)).count() << " " << PSTR(UNITS) << " - ";
- std::cout << "mean: " << r2.first << " " << PSTR(UNITS) << "\terror: " << r2.second << " " << PSTR(UNITS) << std::endl;
- #endif
- #ifndef NOLUT
- std::cout << "(LUT) Total: " << chro::duration_cast<t_timeunits>(chro::duration<t_units>(t3_1-t3_0)).count() << " " << PSTR(UNITS) << " - ";
- std::cout << "mean: " << r3.first << " " << PSTR(UNITS) << "\terror: " << r3.second << " " << PSTR(UNITS) << std::endl;
- #endif
- std::cout << "-------------------------------------------------------------------" << std::endl;
- std::cout << "Precision: " << chro::high_resolution_clock::rep(1) << " ns" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement