fork download
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. __attribute__ ((noinline)) int f(int x) {
  7. return x;
  8. }
  9. __attribute__ ((noinline)) int g(const int& x) {
  10. return x;
  11. }
  12.  
  13. const unsigned N = 200000000;
  14.  
  15. int main() {
  16. clock_t start, end, loop_time;
  17. unsigned throwaway = 0;
  18.  
  19. start = clock();
  20. for (unsigned i = 0; i < N; ++i) {
  21. throwaway += i;
  22. }
  23. end = clock();
  24. cout << "Loop time: " << (end - start) << " ticks" << endl;
  25.  
  26. start = clock();
  27. for (unsigned i = 0; i < N; ++i) {
  28. throwaway += f(i);
  29. throwaway -= i;
  30. }
  31. end = clock();
  32. cout << "By value: " << (end - start) << " ticks" << endl;
  33.  
  34. start = clock();
  35. for (unsigned i = 0; i < N; ++i) {
  36. throwaway += g(i);
  37. throwaway -= i;
  38. }
  39. end = clock();
  40. cout << "By ref: " << (end - start) << " ticks" << endl;
  41.  
  42. cout << "Throwaway (to stop optimisation) = " << throwaway << endl;
  43. return 0;
  44. }
Success #stdin #stdout 1.55s 2680KB
stdin
Standard input is empty
stdout
Loop time: 190000 ticks
By value: 540000 ticks
By ref: 810000 ticks
Throwaway (to stop optimisation) = 3649838848