fork download
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib> //for random
  5. #include <cmath> //for pow
  6. #include <chrono>
  7.  
  8. int main()
  9. {
  10. size_t seed=rand(); //The testbench will be different everytime, but the testbench will be the same for both paths.
  11. static const size_t NUM_ITERS=3000000;
  12. std::chrono::time_point<std::chrono::system_clock> start1, end1,start2,end2;
  13. uint32_t finhash;
  14.  
  15. //Pow
  16. srand(seed);
  17. finhash=0;
  18. start1 = std::chrono::system_clock::now();
  19. for(size_t i=0;i<NUM_ITERS;i++)
  20. {
  21. uint32_t n=rand() & 0x1F;
  22. finhash^=(uint32_t)pow(2,n);
  23. }
  24. end1 = std::chrono::system_clock::now();
  25. std::chrono::duration<double> elapsed_seconds1 = end1-start1;
  26. std::cout << "Pow elapsed time: " << elapsed_seconds1.count() << "s. Hash was " << finhash << std::endl;
  27.  
  28. //leftshift
  29. srand(seed);
  30. finhash=0;
  31. start2 = std::chrono::system_clock::now();
  32. for(size_t i=0;i<NUM_ITERS;i++)
  33. {
  34. uint32_t n=rand() & 0x1F;
  35. finhash^=(uint32_t)(1 << n);
  36. }
  37. end2 = std::chrono::system_clock::now();
  38. std::chrono::duration<double> elapsed_seconds2 = end2-start2;
  39. std::cout << "LeftShift elapsed time: " << elapsed_seconds2.count() << "s. Hash was " << finhash << std::endl;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.37s 3456KB
stdin
Standard input is empty
stdout
Pow elapsed time: 0.301115s.  Hash was 1193342845
LeftShift elapsed time: 0.0688188s.  Hash was 1193342845