language: C++ 4.7.2 (gcc-4.7.2)
date: 228 days 3 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <time.h>
 
using namespace std;
 
__attribute__ ((noinline)) int f(int x) {
        return x;
}
__attribute__ ((noinline)) int g(const int& x) {
        return x;
}
 
const unsigned N = 200000000;
 
int main() {
        clock_t start, end, loop_time;
        unsigned throwaway = 0;
        
        start = clock();
        for (unsigned i = 0; i < N; ++i) {
                throwaway += i;
        }
        end = clock();
        cout << "Loop time: " << (end - start) << " ticks" << endl;
        
        start = clock();
        for (unsigned i = 0; i < N; ++i) {
                throwaway += f(i);
                throwaway -= i;
        }
        end = clock();
        cout << "By value: " << (end - start) << " ticks" << endl;
        
        start = clock();
        for (unsigned i = 0; i < N; ++i) {
                throwaway += g(i);
                throwaway -= i;
        }
        end = clock();
        cout << "By ref: " << (end - start) << " ticks" << endl;
        
        cout << "Throwaway (to stop optimisation) = " << throwaway << endl;
        return 0;
}