fork(1) download
  1. #include <iostream>
  2. #include <sys/time.h> // gettimeofday()
  3. #include <climits> // UINT_MAX
  4.  
  5. using namespace std;
  6.  
  7. #define NUMSTEPS UINT_MAX
  8.  
  9. int main(){
  10. timeval t1, t2, t3, t4;
  11. unsigned int a = 32, b = 64, c = 0, i = 0;
  12. double elapsedTime;
  13.  
  14. gettimeofday(&t1, NULL);
  15. for(i = 0; i < NUMSTEPS; ++i){
  16. swap(a, b);
  17. }
  18.  
  19. gettimeofday(&t2, NULL);
  20. for(i = 0; i < NUMSTEPS; ++i){
  21. a^=b;
  22. b^=a;
  23. a^=b;
  24. }
  25.  
  26. gettimeofday(&t3, NULL);
  27. for(i = 0; i < NUMSTEPS; ++i){
  28. c = b;
  29. b = a;
  30. a = c;
  31. }
  32. gettimeofday(&t4, NULL);
  33.  
  34. cout<<"NUMSTEPS = "<<NUMSTEPS<<endl;
  35. // compute and print the elapsed time in millisec
  36. elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
  37. elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
  38. cout << "swap\t" << elapsedTime << " ms.\n";
  39.  
  40. elapsedTime = (t3.tv_sec - t2.tv_sec) * 1000.0; // sec to ms
  41. elapsedTime += (t3.tv_usec - t2.tv_usec) / 1000.0; // us to ms
  42. cout << "XOR\t" << elapsedTime << " ms.\n";
  43.  
  44. elapsedTime = (t4.tv_sec - t3.tv_sec) * 1000.0; // sec to ms
  45. elapsedTime += (t4.tv_usec - t3.tv_usec) / 1000.0; // us to ms
  46. cout << "+var\t" << elapsedTime << " ms.\n";
  47. cout<<a<<b<<c<<"\b\b\b\b\b\b";
  48. return 0;
  49. }
  50.  
Time limit exceeded #stdin #stdout 5s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty