-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_speed.cpp
140 lines (113 loc) · 3.86 KB
/
test_speed.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "baudvine/deque_ringbuf.h"
#include "baudvine/ringbuf.h"
#include "black_box.h"
#include <gtest/gtest.h>
#include <chrono>
// Speed comparison between deque and standard. Standard should generally be at
// least as fast as deque, but in practice we're not the only process so there's
// going to be some noise.
namespace std {
namespace chrono {
std::ostream& operator<<(std::ostream& os, system_clock::duration d) {
return os << duration_cast<microseconds>(d).count() << " µs";
}
} // namespace chrono
} // namespace std
namespace {
constexpr uint64_t kTestSize = 1 << 25;
std::chrono::system_clock::duration TimeIt(const std::function<void()>& fn) {
const auto start = std::chrono::system_clock::now();
fn();
return std::chrono::system_clock::now() - start;
}
} // namespace
TEST(Speed, PushBackToFull) {
baudvine::RingBuf<uint64_t, kTestSize> standard;
baudvine::DequeRingBuf<uint64_t, kTestSize> deque;
// Preload everything once so all the memory is definitely allocated
for (uint32_t i = 0; i < standard.max_size(); i++) {
standard.push_back(black_box(i));
}
standard.clear();
for (uint32_t i = 0; i < deque.max_size(); i++) {
deque.push_back(black_box(i));
}
deque.clear();
auto standardDuration = TimeIt([&standard] {
for (uint32_t i = 0; i < standard.max_size(); i++) {
standard.push_back(black_box(i));
}
do_nothing(&standard);
});
auto dequeDuration = TimeIt([&deque] {
for (uint32_t i = 0; i < deque.max_size(); i++) {
deque.push_back(black_box(i));
}
do_nothing(&deque);
});
EXPECT_LT(standardDuration, dequeDuration);
std::cout << "RingBuf: " << standardDuration << std::endl;
std::cout << "DequeRingBuf: " << dequeDuration << std::endl;
}
TEST(Speed, PushBackOverFull) {
baudvine::RingBuf<uint64_t, 3> standard;
baudvine::DequeRingBuf<uint64_t, 3> deque;
auto standardDuration = TimeIt([&standard] {
for (uint32_t i = 0; i < kTestSize; i++) {
standard.push_back(black_box(i));
}
do_nothing(&standard);
});
auto dequeDuration = TimeIt([&deque] {
for (uint32_t i = 0; i < kTestSize; i++) {
deque.push_back(black_box(i));
}
do_nothing(&deque);
});
EXPECT_LT(standardDuration, dequeDuration);
std::cout << "RingBuf: " << standardDuration << std::endl;
std::cout << "DequeRingBuf: " << dequeDuration << std::endl;
}
TEST(Speed, IterateOver) {
baudvine::RingBuf<uint64_t, kTestSize> standard;
baudvine::DequeRingBuf<uint64_t, kTestSize> deque;
for (uint32_t i = 0; i < standard.max_size(); i++) {
standard.push_back(black_box(i));
}
for (uint32_t i = 0; i < deque.max_size(); i++) {
deque.push_back(black_box(i));
}
auto standardDuration = TimeIt([&standard] {
for (auto& x : standard) {
black_box(x);
}
});
auto dequeDuration = TimeIt([&deque] {
for (auto& x : deque) {
black_box(x);
}
});
EXPECT_LT(standardDuration, dequeDuration);
std::cout << "RingBuf: " << standardDuration << std::endl;
std::cout << "DequeRingBuf: " << dequeDuration << std::endl;
}
TEST(Speed, Copy) {
// baudvine::copy should be faster than std::copy as std::copy doesn't know
// that there are at most two contiguous sections.
baudvine::RingBuf<int, kTestSize> underTest;
std::fill_n(std::back_inserter(underTest), kTestSize, 55);
std::vector<int> copy;
copy.reserve(kTestSize);
std::fill_n(std::back_inserter(copy), kTestSize, 44);
auto customTime = TimeIt([&underTest, ©] {
baudvine::copy(underTest.begin(), underTest.end(), copy.begin());
do_nothing(©);
});
auto standardTime = TimeIt([&underTest, ©] {
std::copy(underTest.begin(), underTest.end(), copy.begin());
do_nothing(©);
});
EXPECT_LT(customTime, standardTime);
std::cout << "baudvine::copy: " << customTime << std::endl;
std::cout << "std::copy: " << standardTime << std::endl;
}