1 #include <random>
2
3 #include <celero/Celero.h>
4
5 ///
6 /// This is the main(int argc, char** argv) for the entire celero program.
7 /// You can write your own, or use this macro to insert the standard one into the project.
8 ///
9 CELERO_MAIN
10
11 ///
12 /// An additional demo that was created for a Stack Overflow question:
13 ///
14 /// http://stackoverflow.com/questions/15770049/micro-optimizing-a-c-comparison-function/
15 ///
16 /// The code here has been updated from the original post to utilize std::random for better sampling.
17 ///
18 class StackOverflowFixture : public celero::TestFixture
19 {
20 public:
StackOverflowFixture()21 StackOverflowFixture() : Gen(RandomDevice()), RandomBool(0.5), RandomInt(-100, 100)
22 {
23 }
24
NoOp(bool,int,int)25 inline bool NoOp(bool, int, int)
26 {
27 return true;
28 }
29
Compare(bool greater,int p1,int p2)30 inline bool Compare(bool greater, int p1, int p2)
31 {
32 if(greater == true)
33 {
34 return (p1 >= p2);
35 }
36
37 return (p1 <= p2);
38 }
39
40 // Code for Compare 2
Compare2(bool greater,int p1,int p2)41 inline bool Compare2(bool greater, int p1, int p2)
42 {
43 bool ret[2] = {p1 <= p2, p1 >= p2};
44 return ret[greater];
45 }
46 // END
47
48 // Code for Compare 3
Compare3(bool greater,int p1,int p2)49 inline bool Compare3(bool greater, int p1, int p2)
50 {
51 return (!greater != !(p1 <= p2)) | (p1 == p2);
52 }
53 // END
54
Compare4(bool greater,int p1,int p2)55 inline bool Compare4(bool greater, int p1, int p2)
56 {
57 return (greater ^ (p1 <= p2)) | (p1 == p2);
58 }
59
60 // Improved with Bernoulli distribution of booleans.
61 std::random_device RandomDevice;
62 std::mt19937 Gen;
63
64 // give "true" 1/2 of the time
65 std::bernoulli_distribution RandomBool;
66 std::uniform_int_distribution<int> RandomInt;
67 };
68
69 static const int SamplesCount = 10000;
70 static const int IterationsCount = 2000;
71
72 // Shortened up the name from "StackOverflowSimpleComparison"
BASELINE_F(SOSimpleComparison,Baseline,StackOverflowFixture,SamplesCount,IterationsCount)73 BASELINE_F(SOSimpleComparison, Baseline, StackOverflowFixture, SamplesCount, IterationsCount)
74 {
75 celero::DoNotOptimizeAway(NoOp(this->RandomBool(this->Gen), this->RandomInt(this->Gen), this->RandomInt(this->Gen)));
76 }
77
BENCHMARK_F(SOSimpleComparison,Compare,StackOverflowFixture,SamplesCount,IterationsCount)78 BENCHMARK_F(SOSimpleComparison, Compare, StackOverflowFixture, SamplesCount, IterationsCount)
79 {
80 celero::DoNotOptimizeAway(Compare(this->RandomBool(this->Gen), this->RandomInt(this->Gen), this->RandomInt(this->Gen)));
81 }
82
BENCHMARK_F(SOSimpleComparison,Compare2,StackOverflowFixture,SamplesCount,IterationsCount)83 BENCHMARK_F(SOSimpleComparison, Compare2, StackOverflowFixture, SamplesCount, IterationsCount)
84 {
85 celero::DoNotOptimizeAway(Compare2(this->RandomBool(this->Gen), this->RandomInt(this->Gen), this->RandomInt(this->Gen)));
86 }
87
BENCHMARK_F(SOSimpleComparison,Compare3,StackOverflowFixture,SamplesCount,IterationsCount)88 BENCHMARK_F(SOSimpleComparison, Compare3, StackOverflowFixture, SamplesCount, IterationsCount)
89 {
90 celero::DoNotOptimizeAway(Compare3(this->RandomBool(this->Gen), this->RandomInt(this->Gen), this->RandomInt(this->Gen)));
91 }
92
BENCHMARK_F(SOSimpleComparison,Compare4,StackOverflowFixture,SamplesCount,IterationsCount)93 BENCHMARK_F(SOSimpleComparison, Compare4, StackOverflowFixture, SamplesCount, IterationsCount)
94 {
95 celero::DoNotOptimizeAway(Compare4(this->RandomBool(this->Gen), this->RandomInt(this->Gen), this->RandomInt(this->Gen)));
96 }
97