Nugget
EASTLBenchmark.h
1 // Copyright (c) Electronic Arts Inc. All rights reserved.
4 
5 
6 #ifndef EASTLBENCHMARK_H
7 #define EASTLBENCHMARK_H
8 
9 
10 // Intrinsic control
11 //
12 // Our benchmark results are being skewed by inconsistent decisions by the
13 // VC++ compiler to use intrinsic functions. Additionally, many of our
14 // benchmarks work on large blocks of elements, whereas intrinsics often
15 // are an improvement only over small blocks of elements. As a result,
16 // enabling of intrinsics is often resulting in poor benchmark results for
17 // code that gets an intrinsic enabled for it, even though it will often
18 // happen in real code to be the opposite case. The disabling of intrinsics
19 // here often results in EASTL performance being lower than it would be in
20 // real-world situations.
21 //
22 #include <string.h>
23 #ifdef _MSC_VER
24  #pragma function(strlen, strcmp, strcpy, strcat, memcpy, memcmp, memset)
25 #endif
26 
27 
28 #include <EASTL/set.h>
29 #include <EASTL/string.h>
30 #include <EAStdC/EAStopwatch.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 
35 void BenchmarkSort();
36 void BenchmarkList();
37 void BenchmarkString();
38 void BenchmarkVector();
39 void BenchmarkDeque();
40 void BenchmarkSet();
41 void BenchmarkMap();
42 void BenchmarkHash();
43 void BenchmarkAlgorithm();
44 void BenchmarkHeap();
45 void BenchmarkBitset();
46 void BenchmarkTupleVector();
47 
48 
49 namespace Benchmark
50 {
51 
52  // Environment
53  //
54  // The environment for this benchmark test.
55  //
56  struct Environment
57  {
58  eastl::string8 msPlatform; // Name of test platform (e.g. "Windows")
59  eastl::string8 msSTLName1; // Name of competitor #1 (e.g. "EASTL").
60  eastl::string8 msSTLName2; // Name of competitor #2 (e.g. "MS STL").
61 
62  void clear() { msPlatform.set_capacity(0); msSTLName1.set_capacity(0); msSTLName2.set_capacity(0); }
63  };
64 
65  Environment& GetEnvironment();
66 
67 
68  // Result
69  //
70  // An individual benchmark result.
71  //
72  struct Result
73  {
74  eastl::string8 msName; // Test name (e.g. "vector/insert").
75  int mUnits; // Timing units (e.g. EA::StdC::Stopwatch::kUnitsSeconds).
76  int64_t mTime1; // Time of competitor #1.
77  uint64_t mTime1NS; // Nanoseconds.
78  int64_t mTime2; // Time of competitor #2.
79  int64_t mTime2NS; // Nanoseconds.
80  eastl::string8 msNotes; // Any comments to attach to this result.
81 
82  Result() : msName(), mUnits(EA::StdC::Stopwatch::kUnitsCPUCycles),
83  mTime1(0), mTime1NS(0), mTime2(0), mTime2NS(0), msNotes() { }
84  };
85 
86  inline bool operator<(const Result& r1, const Result& r2)
87  { return r1.msName < r2.msName; }
88 
89  typedef eastl::set<Result> ResultSet;
90 
91  ResultSet& GetResultSet();
92 
93 
94  // Scratch sprintf buffer
95  extern char gScratchBuffer[1024];
96 
97 
98 
99  // Utility functions
100  //
101  void DoNothing(...);
102  void AddResult(const char* pName, int units, int64_t nTime1, int64_t nTime2, const char* pNotes = NULL);
103  void PrintResults();
104  void WriteTime(int64_t timeNS, eastl::string& sTime);
105 
106 
107 } // namespace Benchmark
108 
109 
110 
111 
121 {
122  int32_t mData[2048];
123 };
124 
125 struct LargePOD
126 {
127  LargeObject mLargeObject1;
128  LargeObject mLargeObject2;
129  const char* mpName1;
130  const char* mpName2;
131 
132  explicit LargePOD(int32_t x = 0) // A true POD doesn't have a non-trivial constructor.
133  {
134  memset(mLargeObject1.mData, 0, sizeof(mLargeObject1.mData));
135  memset(mLargeObject2.mData, 0, sizeof(mLargeObject2.mData));
136  mLargeObject1.mData[0] = x;
137 
138  mpName1 = "LargePOD1";
139  mpName2 = "LargePOD2";
140  }
141 
142  LargePOD(const LargePOD& largePOD) // A true POD doesn't have a non-trivial copy-constructor.
143  : mLargeObject1(largePOD.mLargeObject1),
144  mLargeObject2(largePOD.mLargeObject2),
145  mpName1(largePOD.mpName1),
146  mpName2(largePOD.mpName2)
147  {
148  }
149 
150  virtual ~LargePOD() { }
151 
152  LargePOD& operator=(const LargePOD& largePOD) // A true POD doesn't have a non-trivial assignment operator.
153  {
154  if(&largePOD != this)
155  {
156  mLargeObject1 = largePOD.mLargeObject1;
157  mLargeObject2 = largePOD.mLargeObject2;
158  mpName1 = largePOD.mpName1;
159  mpName2 = largePOD.mpName2;
160  }
161  return *this;
162  }
163 
164  virtual void DoSomething() // Note that by declaring this virtual, this class is not truly a POD.
165  { // But it acts like a POD for the purposes of EASTL algorithms.
166  mLargeObject1.mData[1]++;
167  }
168 
169  operator int()
170  {
171  return (int)mLargeObject1.mData[0];
172  }
173 };
174 
175 //EASTL_DECLARE_POD(LargePOD);
176 //EASTL_DECLARE_TRIVIAL_CONSTRUCTOR(LargePOD);
177 //EASTL_DECLARE_TRIVIAL_COPY(LargePOD);
178 //EASTL_DECLARE_TRIVIAL_ASSIGN(LargePOD);
179 //EASTL_DECLARE_TRIVIAL_DESTRUCTOR(LargePOD);
180 //EASTL_DECLARE_TRIVIAL_RELOCATE(LargePOD);
181 
182 // Operators
183 // We specifically define only == and <, in order to verify that
184 // our containers and algorithms are not mistakenly expecting other
185 // operators for the contained and manipulated classes.
186 inline bool operator==(const LargePOD& t1, const LargePOD& t2)
187 {
188  return (memcmp(&t1.mLargeObject1, &t2.mLargeObject1, sizeof(t1.mLargeObject1)) == 0) &&
189  (memcmp(&t1.mLargeObject2, &t2.mLargeObject2, sizeof(t1.mLargeObject2)) == 0) &&
190  (strcmp(t1.mpName1, t2.mpName1) == 0) &&
191  (strcmp(t1.mpName2, t2.mpName2) == 0);
192 }
193 
194 inline bool operator<(const LargePOD& t1, const LargePOD& t2)
195 {
196  return (memcmp(&t1.mLargeObject1, &t2.mLargeObject1, sizeof(t1.mLargeObject1)) < 0) &&
197  (memcmp(&t1.mLargeObject2, &t2.mLargeObject2, sizeof(t1.mLargeObject2)) < 0) &&
198  (strcmp(t1.mpName1, t2.mpName1) < 0) &&
199  (strcmp(t1.mpName2, t2.mpName2) < 0);
200 }
201 
202 
203 
204 
205 
206 #endif // Header sentry
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
Definition: string.h:280
Definition: set.h:85
Definition: EASTLBenchmark.h:57
Definition: EASTLBenchmark.h:73
Definition: EASTLBenchmark.h:121
Definition: EASTLBenchmark.h:126