fork(7) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <stdint.h>
  5. #include <random>
  6. #include <time.h>
  7. #include <string.h>
  8. #include <chrono>
  9.  
  10. using namespace std;
  11. uint64_t itr = 1000000;//10 Billion
  12. int len = 100;
  13.  
  14. class Timer {
  15. std::chrono::steady_clock::time_point start;
  16. std::chrono::steady_clock::time_point stop;
  17. public:
  18. Timer(){
  19. start = std::chrono::steady_clock::now();
  20. stop = std::chrono::steady_clock::now();
  21. }
  22. virtual ~Timer() {}
  23.  
  24. inline void begin() {
  25. start = std::chrono::steady_clock::now();
  26. }
  27.  
  28. inline void end() {
  29. stop = std::chrono::steady_clock::now();
  30. }
  31.  
  32. inline double elapsedMillis() {
  33. return std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
  34. }
  35.  
  36. inline double elapsedMicro() {
  37. return std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
  38. }
  39.  
  40. inline double elapsedNano() {
  41. return std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count();
  42. }
  43.  
  44. inline double elapsedSec() {
  45. auto diff = stop - start;
  46. return std::chrono::duration<double> (diff).count();
  47. }
  48. };
  49.  
  50. int main(int argc) {
  51. string s[] = { {len,argc+'A'}, {len,argc+'A'}, {len, argc+'B'}, {len, argc+'B'} };
  52. std::cout << "s[0] " << s[0] << '\n';
  53. uint64_t a = 0;
  54. Timer t;
  55. for (int x = 0; x < 2; ++x)
  56. {
  57. t.begin();
  58. for(uint64_t i =0;i<itr;i++){
  59. if(s[i&3] == s[(i+1)&3])
  60. a += i;
  61. }
  62. t.end();
  63.  
  64. cout<<"== took:"<<t.elapsedMillis()<<endl;
  65.  
  66. t.begin();
  67. for(uint64_t i =0;i<itr;i++){
  68. if(s[i&3].compare(s[(i+1)&3])==0)
  69. a += i;
  70. }
  71. t.end();
  72.  
  73. cout<<".compare took:"<<t.elapsedMillis()<<endl;
  74.  
  75. t.begin();
  76. for(uint64_t i =0;i<itr;i++){
  77. if(strcmp(s[i&3].c_str(),s[(i+1)&3].c_str()))
  78. a += i;
  79. }
  80. t.end();
  81.  
  82. cout<<"strcmp took:"<<t.elapsedMillis()<<endl;
  83. }
  84. if (a == 42) std::cout << "huh?\n";
  85. }
Success #stdin #stdout 0.12s 3232KB
stdin
Standard input is empty
stdout
s[0] dB
==       took:21
.compare took:21
strcmp   took:14
==       took:21
.compare took:25
strcmp   took:14