Bridges-C++  3.2.0
Bridges(C++API)
SortingBenchmark.h
Go to the documentation of this file.
1 #ifndef SORTINGBENCHMARK_H
2 #define SORTINGBENCHMARK_H
3 
4 #include <LineChart.h>
5 #include <limits>
6 #include <vector>
7 #include <chrono>
8 #include <stdlib.h>
9 
10 namespace bridges {
11  namespace benchmark {
12  using namespace bridges::datastructure;
13 
48  private:
49  LineChart& plot;
50 
51  int maxSize;
52  int baseSize;
53  int increment;
54  double geoBase;
55  double time_cap;
56 
57  void generate(int* arr, int n) {
58  for (int i = 0; i < n; i++) {
59  arr[i] = ((double)rand()) / RAND_MAX * (2 * n);
60  }
61  }
62 
63  bool check (int* arr, int n) {
64  bool ok = true;
65  for (int i = 1; i < n; ++i) {
66  if (arr[i] < arr[i - 1]) {
67  ok = false;
68  }
69  }
70  return ok;
71  }
72 
73  public:
75  : plot (p) {
76  p.setXLabel("Size of Array");
77  p.setYLabel("Runtime (in s)");
78 
79  //r = new Random();
80 
81  maxSize = 1;
82  baseSize = 1;
83  increment = 1;
84  geoBase = 1.;
85  time_cap = std::numeric_limits<double>::max();
86  }
87 
93  void setMaxSize(int size) {
94  maxSize = size;
95  }
96 
102  void setBaseSize(int size) {
103  baseSize = size;
104  }
105 
106 
112  void setIncrement(int inc) {
113  increment = inc;
114  }
115 
121  void setGeometric(double base) {
122  geoBase = base;
123  }
124 
136  void linearRange(int baseSize, int maxSize, int nbPoint) {
137  setBaseSize (baseSize);
138  setMaxSize (maxSize);
139  setIncrement ((maxSize - baseSize) / nbPoint);
140  setGeometric (1.0);
141  }
142 
155  void geometricRange(int baseSize, int maxSize, double base) {
156  setBaseSize (baseSize);
157  setMaxSize (maxSize);
158  setIncrement (0);
159  setGeometric (base);
160  if (base <= 1.0) {
161  std::cerr << "base should be > 1.0\n";
162  }
163  }
164 
174  void setTimeCap(double cap_in_s) {
175  time_cap = cap_in_s;
176  }
177 
184  void run(std::string algoName, void (*runnable)(int*, int)) {
185  std::vector<double> time;
186  std::vector<double> xData;
187 
188  // System.out.println(geoBase);
189  // System.out.println(increment);
190 
191  for (int n = baseSize; n <= maxSize;
192  n = std::max((int)(geoBase * n) + increment, n + 1)) {
193 
194  //System.out.println(n);
195  std::vector<int> arr(n);
196 
197  generate(&arr[0], n);
198 
199  std::chrono::time_point<std::chrono::system_clock> start = std::chrono::system_clock::now();
200 
201  runnable(&arr[0], n);
202 
203  std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();
204 
205  std::chrono::duration<double> elapsed_seconds = end - start;
206 
207  if (! check(&arr[0], n)) {
208  std::cerr << "Sorting algorithm " << algoName << " is incorrect\n";
209  }
210 
211  time.push_back ((double)elapsed_seconds.count() );
212  xData.push_back ( (double)n );
213 
214  if (elapsed_seconds.count() > time_cap) {
215  break;
216  }
217  }
218  plot.setXData(algoName, xData);
219  plot.setYData(algoName, time);
220  }
221 
222  };
223  }
224 }
225 
226 #endif
void setXLabel(string xaxisName)
Change the label for the X-axis.
Definition: LineChart.h:184
void setTimeCap(double cap_in_s)
sets an upper bound to the time of a run.
Definition: SortingBenchmark.h:174
void setGeometric(double base)
Sets a geometric progression for the benchmark size.
Definition: SortingBenchmark.h:121
void geometricRange(int baseSize, int maxSize, double base)
The benchmark will sample a range using in geometrically increasing sequence.
Definition: SortingBenchmark.h:155
Show series of data or functions using a line chart.
Definition: LineChart.h:43
Benchmarks sorting algorithm.
Definition: SortingBenchmark.h:47
void setXData(string series, vector< double > xdata)
Changes the X data for a series.
Definition: LineChart.h:217
void setMaxSize(int size)
Puts a cap on the largest array to be used.
Definition: SortingBenchmark.h:93
void setBaseSize(int size)
Smallest array to be used.
Definition: SortingBenchmark.h:102
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
void run(std::string algoName, void(*runnable)(int *, int))
benchmark one implementation
Definition: SortingBenchmark.h:184
void linearRange(int baseSize, int maxSize, int nbPoint)
The benchmark will sample a range with a fixed number of points.
Definition: SortingBenchmark.h:136
void setIncrement(int inc)
Sets the increment for the benchmark size.
Definition: SortingBenchmark.h:112
SortingBenchmark(LineChart &p)
Definition: SortingBenchmark.h:74
void setYData(string series, vector< double > ydata)
Changes the Y data for a series.
Definition: LineChart.h:237
void setYLabel(string yaxisName)
Change the label for the Y-axis.
Definition: LineChart.h:166
Definition: Array.h:9