-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl_stall.cpp
81 lines (64 loc) · 2.01 KB
/
stl_stall.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
74
75
76
77
78
79
80
81
#include <vector>
#include <iostream>
#include <algorithm>
#include <windows.h>
// Êëàññ Abc íàì íóæåí äëÿ òîãî, ÷òîáû ñäåëàòü ó íåãî ôóíêöèþ,
// êîòîðàÿ áû íèêîãäà íå çàèíëàéíèëàñü. Áëàãîäàðÿ ýòîìó, íå
// èíëàéíÿòñÿ âûçîâû lower_bound â main.
class Abc
{
public:
virtual ~Abc() {}
virtual int compare(int a, int b) { return a < b; }
};
Abc g_abc;
Abc *g_pAbc = &g_abc;
struct MyStruct
{
int a;
friend int operator<(const MyStruct &l, const MyStruct &r)
{
return g_pAbc->compare(l.a,r.a);
}
};
// MyPred1 ÿâëÿåòñÿ áðàòîì-áëèçíåöîì std::less.
// Ïðèâåäåíà çäåñü äëÿ íàãëÿäíîñòè.
struct MyPred1
{
template<typename T1, typename T2>
int operator()(const T1 &l, const T2 &r) const { return l < r; }
};
// Åäèíñòâåííîå îòëè÷èå MyPred2 îò MyPred1 -- ýòî íàëè÷èå ÷ëåíà int a â MyPred2.
struct MyPred2
{
int a;
template<typename T1, typename T2>
int operator()(const T1 &l, const T2 &r) const { return l < r; }
};
void main()
{
const size_t count = 10*1024*1024;
// Âåêòîð, â êîòîðîì áóäåì èñêàòü, ïóñòîé, ÷òîáû ñäåëàòü ðàçíèöó îùóòèìîé.
std::vector<MyStruct> t;
LARGE_INTEGER t1,t2,t3;
// Õîëîñòîé ïðîãîí.
for(size_t a0 = 0; a0 < count; ++a0)
{
std::vector<MyStruct>::iterator i0 = std::lower_bound(t.begin(),t.end(),MyStruct(),MyPred1());
}
QueryPerformanceCounter(&t1);
// Âàðèàíò 1.
for(size_t a1 = 0; a1 < count; ++a1)
{
std::vector<MyStruct>::iterator i1 = std::lower_bound(t.begin(),t.end(),MyStruct(),MyPred1());
}
QueryPerformanceCounter(&t2);
// Âàðèàíò 2.
for(size_t a2 = 0; a2 < count; ++a2)
{
std::vector<MyStruct>::iterator i2 = std::lower_bound(t.begin(),t.end(),MyStruct(),MyPred2());
}
QueryPerformanceCounter(&t3);
std::cout << t2.QuadPart - t1.QuadPart << " " << t3.QuadPart - t2.QuadPart << "\n";
std::cout << (double)(t2.QuadPart - t1.QuadPart)/(double)(t3.QuadPart - t2.QuadPart) << "\n";
}