Advertisement
syj

Untitled

syj
May 17th, 2021
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. int loop = 3000000;
  2. unsigned long long test=0b10000;    
  3. int expected = 1;
  4.  
  5. void test1() {
  6. auto start_time = std::chrono::high_resolution_clock::now();
  7.     for(int x=0;x < loop;x++) {
  8.         unsigned long long t=test;    
  9.         int z = 0;
  10.         while(t)
  11.         {
  12.             if(t&1) z++;
  13.             t=t>>1;
  14.         }
  15.         if(z!=expected) {
  16.             cerr<<"error"<<endl;
  17.         }
  18.     }
  19.     auto end_time = std::chrono::high_resolution_clock::now();
  20.      auto time = end_time - start_time;
  21.     cerr << time/std::chrono::milliseconds(1) << "ms to run.\n";
  22. }
  23.  
  24. void test2() {
  25.      auto start_time = std::chrono::high_resolution_clock::now();
  26.     for(int x=0;x < loop;x++) {
  27.         unsigned long long t=test;
  28.         int z = 0;
  29.         for(int i; t; t=t^1ULL<<i)
  30.         {
  31.             i=__builtin_ctzll(t);
  32.             z++;            
  33.         }
  34.         if(z!=expected) {
  35.             cerr<<"error"<<endl;
  36.         }
  37.     }
  38.     auto end_time = std::chrono::high_resolution_clock::now();
  39.      auto time = end_time - start_time;
  40.     cerr << time/std::chrono::milliseconds(1) << "ms to run.\n";
  41. }
  42.  
  43. void test3() {
  44.      auto start_time = std::chrono::high_resolution_clock::now();
  45.     for(int x=0;x < loop;x++) {
  46.         unsigned long long t=test;
  47.         int z = 0;
  48.         for(int i=0; t; t=t>>(i+1))
  49.         {
  50.             i=__builtin_ctzll(t);
  51.             z++;            
  52.         }
  53.         if(z!=expected) {
  54.             cerr<<"error"<<endl;
  55.         }
  56.     }
  57.     auto end_time = std::chrono::high_resolution_clock::now();
  58.      auto time = end_time - start_time;
  59.     cerr << time/std::chrono::milliseconds(1) << "ms to run.\n";
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement