Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int loop = 3000000;
- unsigned long long test=0b10000;
- int expected = 1;
- void test1() {
- auto start_time = std::chrono::high_resolution_clock::now();
- for(int x=0;x < loop;x++) {
- unsigned long long t=test;
- int z = 0;
- while(t)
- {
- if(t&1) z++;
- t=t>>1;
- }
- if(z!=expected) {
- cerr<<"error"<<endl;
- }
- }
- auto end_time = std::chrono::high_resolution_clock::now();
- auto time = end_time - start_time;
- cerr << time/std::chrono::milliseconds(1) << "ms to run.\n";
- }
- void test2() {
- auto start_time = std::chrono::high_resolution_clock::now();
- for(int x=0;x < loop;x++) {
- unsigned long long t=test;
- int z = 0;
- for(int i; t; t=t^1ULL<<i)
- {
- i=__builtin_ctzll(t);
- z++;
- }
- if(z!=expected) {
- cerr<<"error"<<endl;
- }
- }
- auto end_time = std::chrono::high_resolution_clock::now();
- auto time = end_time - start_time;
- cerr << time/std::chrono::milliseconds(1) << "ms to run.\n";
- }
- void test3() {
- auto start_time = std::chrono::high_resolution_clock::now();
- for(int x=0;x < loop;x++) {
- unsigned long long t=test;
- int z = 0;
- for(int i=0; t; t=t>>(i+1))
- {
- i=__builtin_ctzll(t);
- z++;
- }
- if(z!=expected) {
- cerr<<"error"<<endl;
- }
- }
- auto end_time = std::chrono::high_resolution_clock::now();
- auto time = end_time - start_time;
- cerr << time/std::chrono::milliseconds(1) << "ms to run.\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement