-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVirtual.cpp
75 lines (53 loc) · 1.22 KB
/
Virtual.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// compile with
// c++-52 -std=c++14 -O2 -Wall Virtual.cpp -fopt-info-vec
//
// comment out the random_shuffle
// try to change the "pattern" in the vector of pointers
//
// change -O2 in -Ofast
// add -funroll-loops ??
#include <cmath>
// base class
struct Base {
virtual ~Base(){}
virtual double comp() const=0;
};
// derived classes
struct A : public Base {
A(){}
explicit A(double ix) : x(ix){}
~A(){}
double comp() const override { return x;}
double x;
};
struct B final : public Base {
B(){}
explicit B(double ix) : x(ix){}
~B(){}
double comp() const override { return x;}
double x;
};
struct C final : public A {
C (){}
explicit C(double ix) : A(ix){}
~C(){}
double comp() const override { return x;}
};
#include<vector>
#include<memory>
#include<random>
#include<algorithm>
int main() {
int size=1000*10;
std::vector<A> va(size,A(3.14));
std::vector<B> vb(size,B(7.1));
std::vector<Base const *> pa; pa.reserve(2*size);
int i=0; for (auto const & a : va) { pa.push_back(&a); pa.push_back(&vb[i++]); }
std::random_shuffle(pa.begin(),pa.end());
double c=0;
for (int i=0; i<10000; ++i) {
for (auto const & p : pa) c += p->comp();
}
return int(c);
}