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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
#include "EASTLBenchmark.h"
#include "EASTLTest.h"
#include <EAStdC/EAStopwatch.h>
#include <EASTL/heap.h>
#include <EASTL/vector.h>
#include <EASTL/algorithm.h>
#ifdef _MSC_VER
#pragma warning(push, 0)
#pragma warning(disable: 4350) // behavior change: X called instead of Y
#endif
#include <algorithm>
#include <vector>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
using namespace EA;
namespace
{
template <typename Iterator>
void TestMakeHeapStd(EA::StdC::Stopwatch& stopwatch, Iterator first, Iterator last)
{
stopwatch.Restart();
std::make_heap(first, last);
stopwatch.Stop();
}
template <typename Iterator>
void TestMakeHeapEa(EA::StdC::Stopwatch& stopwatch, Iterator first, Iterator last)
{
stopwatch.Restart();
eastl::make_heap(first, last);
stopwatch.Stop();
}
template <typename Iterator1, typename Iterator2>
void TestPushHeapStd(EA::StdC::Stopwatch& stopwatch, Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
{
stopwatch.Restart();
while(first2 != last2)
{
*last1++ = *first2++;
std::push_heap(first1, last1);
}
stopwatch.Stop();
}
template <typename Iterator1, typename Iterator2>
void TestPushHeapEa(EA::StdC::Stopwatch& stopwatch, Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
{
stopwatch.Restart();
while(first2 != last2)
{
*last1++ = *first2++;
eastl::push_heap(first1, last1);
}
stopwatch.Stop();
}
template <typename Iterator>
void TestPopHeapStd(EA::StdC::Stopwatch& stopwatch, Iterator first, Iterator last, Iterator popEnd)
{
stopwatch.Restart();
while(last != popEnd)
std::pop_heap(first, last--);
stopwatch.Stop();
}
template <typename Iterator>
void TestPopHeapEa(EA::StdC::Stopwatch& stopwatch, Iterator first, Iterator last, Iterator popEnd)
{
stopwatch.Restart();
while(last != popEnd)
eastl::pop_heap(first, last--);
stopwatch.Stop();
}
template <typename Iterator>
void TestSortHeapStd(EA::StdC::Stopwatch& stopwatch, Iterator first, Iterator last)
{
stopwatch.Restart();
std::sort_heap(first, last);
stopwatch.Stop();
}
template <typename Iterator>
void TestSortHeapEa(EA::StdC::Stopwatch& stopwatch, Iterator first, Iterator last)
{
stopwatch.Restart();
eastl::sort_heap(first, last);
stopwatch.Stop();
}
} // namespace
void BenchmarkHeap()
{
EASTLTest_Printf("Heap (Priority Queue)\n");
EA::UnitTest::RandGenT<uint32_t> rng(EA::UnitTest::GetRandSeed());
EA::StdC::Stopwatch stopwatch1(EA::StdC::Stopwatch::kUnitsCPUCycles);
EA::StdC::Stopwatch stopwatch2(EA::StdC::Stopwatch::kUnitsCPUCycles);
{
const int kArraySize = 100000;
// uint32[]
uint32_t* const pIntArrayS = new uint32_t[kArraySize * 2]; // * 2 because we will be adding more items via push_heap.
uint32_t* const pIntArrayE = new uint32_t[kArraySize * 2]; // S means Std; E means EA.
uint32_t* const pIntArray2 = new uint32_t[kArraySize]; // This will be used for pop_heap.
eastl::generate(pIntArrayS, pIntArrayS + kArraySize, rng);
eastl::copy(pIntArrayS, pIntArrayS + kArraySize, pIntArrayE);
eastl::copy(pIntArrayS, pIntArrayS + kArraySize, pIntArray2);
// vector<TestObject>
std::vector<TestObject> stdVectorTO(kArraySize * 2);
std::vector<TestObject> stdVectorTO2(kArraySize);
eastl::vector<TestObject> eaVectorTO(kArraySize * 2);
eastl::vector<TestObject> eaVectorTO2(kArraySize);
for(int k = 0; k < kArraySize; k++)
{
stdVectorTO[k] = TestObject(pIntArrayS[k]);
stdVectorTO2[k] = TestObject(pIntArrayS[k]);
eaVectorTO[k] = TestObject(pIntArrayS[k]);
eaVectorTO2[k] = TestObject(pIntArrayS[k]);
}
for(int i = 0; i < 2; i++)
{
///////////////////////////////
// Test make_heap
///////////////////////////////
TestMakeHeapStd(stopwatch1, pIntArrayS, pIntArrayS + kArraySize);
TestMakeHeapEa (stopwatch2, pIntArrayE, pIntArrayE + kArraySize);
if(i == 1)
Benchmark::AddResult("heap (uint32_t[])/make_heap", stopwatch1.GetUnits(), stopwatch1.GetElapsedTime(), stopwatch2.GetElapsedTime());
TestMakeHeapStd(stopwatch1, stdVectorTO.begin(), stdVectorTO.begin() + kArraySize);
TestMakeHeapEa (stopwatch2, eaVectorTO.begin(), eaVectorTO.begin() + kArraySize);
if(i == 1)
Benchmark::AddResult("heap (vector<TestObject>)/make_heap", stopwatch1.GetUnits(), stopwatch1.GetElapsedTime(), stopwatch2.GetElapsedTime());
///////////////////////////////
// Test push_heap
///////////////////////////////
TestPushHeapStd(stopwatch1, pIntArrayS, pIntArrayS + kArraySize, pIntArray2, pIntArray2 + kArraySize);
TestPushHeapEa (stopwatch2, pIntArrayE, pIntArrayE + kArraySize, pIntArray2, pIntArray2 + kArraySize);
if(i == 1)
Benchmark::AddResult("heap (uint32_t[])/push_heap", stopwatch1.GetUnits(), stopwatch1.GetElapsedTime(), stopwatch2.GetElapsedTime());
TestPushHeapStd(stopwatch1, stdVectorTO.begin(), stdVectorTO.begin() + kArraySize, stdVectorTO2.begin(), stdVectorTO2.begin() + kArraySize);
TestPushHeapEa (stopwatch2, eaVectorTO.begin(), eaVectorTO.begin() + kArraySize, eaVectorTO2.begin(), eaVectorTO2.begin() + kArraySize);
if(i == 1)
Benchmark::AddResult("heap (vector<TestObject>)/push_heap", stopwatch1.GetUnits(), stopwatch1.GetElapsedTime(), stopwatch2.GetElapsedTime());
///////////////////////////////
// Test pop_heap
///////////////////////////////
TestPopHeapStd(stopwatch1, pIntArrayS, pIntArrayS + (kArraySize * 2), pIntArrayS + kArraySize); // * 2 because we used push_heap above to add more items.
TestPopHeapEa (stopwatch2, pIntArrayE, pIntArrayE + (kArraySize * 2), pIntArrayE + kArraySize);
if(i == 1)
Benchmark::AddResult("heap (uint32_t[])/pop_heap", stopwatch1.GetUnits(), stopwatch1.GetElapsedTime(), stopwatch2.GetElapsedTime());
TestPopHeapStd(stopwatch1, stdVectorTO.begin(), stdVectorTO.begin() + (kArraySize * 2), stdVectorTO.begin() + kArraySize); // * 2 because we used push_heap above to add more items.
TestPopHeapEa (stopwatch2, eaVectorTO.begin(), eaVectorTO.begin() + (kArraySize * 2), eaVectorTO.begin() + kArraySize);
if(i == 1)
Benchmark::AddResult("heap (vector<TestObject>)/pop_heap", stopwatch1.GetUnits(), stopwatch1.GetElapsedTime(), stopwatch2.GetElapsedTime());
///////////////////////////////
// Test sort_heap
///////////////////////////////
TestSortHeapStd(stopwatch1, pIntArrayS, pIntArrayS + kArraySize);
TestSortHeapEa (stopwatch2, pIntArrayE, pIntArrayE + kArraySize);
if(i == 1)
Benchmark::AddResult("heap (uint32_t[])/sort_heap", stopwatch1.GetUnits(), stopwatch1.GetElapsedTime(), stopwatch2.GetElapsedTime());
TestSortHeapStd(stopwatch1, stdVectorTO.begin(), stdVectorTO.begin() + kArraySize);
TestSortHeapEa (stopwatch2, eaVectorTO.begin(), eaVectorTO.begin() + kArraySize);
if(i == 1)
Benchmark::AddResult("heap (vector<TestObject>)/sort_heap", stopwatch1.GetUnits(), stopwatch1.GetElapsedTime(), stopwatch2.GetElapsedTime());
}
delete[] pIntArrayS;
delete[] pIntArrayE;
delete[] pIntArray2;
}
}
|