Bridges-C++  3.4.5-dev1-6-g935685a
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  std::string generatorType;
57 
58  void generateRandom(int* arr, int n) {
59  for (int i = 0; i < n; i++) {
60  arr[i] = ((double)rand()) / RAND_MAX * (2 * n);
61  }
62  }
63 
64  void generateInOrder(int* arr, int n) {
65  for (int i = 0; i < n; i++) {
66  arr[i] = i;
67  }
68  }
69  void generateReverseOrder(int* arr, int n) {
70  for (int i = 0; i < n; i++) {
71  arr[i] = n - i;
72  }
73  }
74  void generateFewValues(int* arr, int n) {
75  for (int i = 0; i < n; i++) {
76  arr[i] = ((double)rand()) / RAND_MAX * (4);
77  }
78  }
79  void generateAlmostSorted(int* arr, int n) {
80  if (n < 20) {
81  generateRandom(arr, n);
82  }
83  else {
84  int i;
85  for (i = 0; i < n - 20; i++) {
86  arr[i] = i;
87  }
88  for (; i < n; i++) {
89  arr[i] = ((double)rand()) / RAND_MAX * (2 * n);
90  }
91  }
92  }
93 
94  void generate(int* arr, int n) {
95  if (generatorType == "random") {
96  generateRandom(arr, n);
97  }
98  else if (generatorType == "inorder") {
99  generateInOrder(arr, n);
100  }
101  else if (generatorType == "reverseorder") {
102  generateReverseOrder(arr, n);
103  }
104  else if (generatorType == "fewdifferentvalues") {
105  generateFewValues(arr, n);
106  }
107  else if (generatorType == "almostsorted") {
108  generateAlmostSorted(arr, n);
109  }
110  else {
111  throw std::string("unknown generator");
112  }
113 
114  }
115 
116  bool check (int* arr, int n) {
117  bool ok = true;
118  for (int i = 1; i < n; ++i) {
119  if (arr[i] < arr[i - 1]) {
120  ok = false;
121  }
122  }
123  return ok;
124  }
125 
126  public:
128  : plot (p) {
129  p.setXLabel("Size of Array");
130  p.setYLabel("Runtime (in s)");
131 
132  //r = new Random();
133 
134  maxSize = 1;
135  baseSize = 1;
136  increment = 1;
137  geoBase = 1.;
138  time_cap = std::numeric_limits<double>::max();
139  setGenerator("random");
140  }
141 
147  void setGenerator(const std::string& generatorName) {
148  generatorType = generatorName;
149  }
150 
151  std::string getGenerator() const {
152  return generatorType;
153  }
159  void setMaxSize(int size) {
160  maxSize = size;
161  }
162 
168  void setBaseSize(int size) {
169  baseSize = size;
170  }
171 
177  void setIncrement(int inc) {
178  increment = inc;
179  }
180 
186  void setGeometric(double base) {
187  geoBase = base;
188  }
189 
201  void linearRange(int baseSize, int maxSize, int nbPoint) {
202  setBaseSize (baseSize);
203  setMaxSize (maxSize);
204  setIncrement ((maxSize - baseSize) / nbPoint);
205  setGeometric (1.0);
206  }
207 
220  void geometricRange(int baseSize, int maxSize, double base) {
221  setBaseSize (baseSize);
222  setMaxSize (maxSize);
223  setIncrement (0);
224  setGeometric (base);
225  if (base <= 1.0) {
226  std::cerr << "base should be > 1.0\n";
227  }
228  }
229 
239  void setTimeCap(double cap_in_s) {
240  time_cap = cap_in_s;
241  }
242 
249  void run(std::string algoName, void (*runnable)(int*, int)) {
250  std::vector<double> time;
251  std::vector<double> xData;
252 
253  // System.out.println(geoBase);
254  // System.out.println(increment);
255 
256  for (int n = baseSize; n <= maxSize;
257  n = std::max((int)(geoBase * n) + increment, n + 1)) {
258 
259  //System.out.println(n);
260  std::vector<int> arr(n);
261 
262  generate(&arr[0], n);
263 
264  std::chrono::time_point<std::chrono::system_clock> start = std::chrono::system_clock::now();
265 
266  runnable(&arr[0], n);
267 
268  std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();
269 
270  std::chrono::duration<double> elapsed_seconds = end - start;
271 
272  if (! check(&arr[0], n)) {
273  std::cerr << "Sorting algorithm " << algoName << " is incorrect\n";
274  }
275 
276  time.push_back ((double)elapsed_seconds.count() );
277  xData.push_back ( (double)n );
278 
279  if (elapsed_seconds.count() > time_cap) {
280  break;
281  }
282  }
283  plot.setXData(algoName, xData);
284  plot.setYData(algoName, time);
285  }
286 
287  };
288  }
289 }
290 
291 #endif
Benchmarks sorting algorithm.
Definition: SortingBenchmark.h:47
void geometricRange(int baseSize, int maxSize, double base)
The benchmark will sample a range using in geometrically increasing sequence.
Definition: SortingBenchmark.h:220
std::string getGenerator() const
Definition: SortingBenchmark.h:151
void setTimeCap(double cap_in_s)
sets an upper bound to the time of a run.
Definition: SortingBenchmark.h:239
void run(std::string algoName, void(*runnable)(int *, int))
benchmark one implementation
Definition: SortingBenchmark.h:249
void linearRange(int baseSize, int maxSize, int nbPoint)
The benchmark will sample a range with a fixed number of points.
Definition: SortingBenchmark.h:201
void setGenerator(const std::string &generatorName)
Definition: SortingBenchmark.h:147
void setMaxSize(int size)
Puts a cap on the largest array to be used.
Definition: SortingBenchmark.h:159
void setGeometric(double base)
Sets a geometric progression for the benchmark size.
Definition: SortingBenchmark.h:186
void setIncrement(int inc)
Sets the increment for the benchmark size.
Definition: SortingBenchmark.h:177
SortingBenchmark(LineChart &p)
Definition: SortingBenchmark.h:127
void setBaseSize(int size)
Smallest array to be used.
Definition: SortingBenchmark.h:168
Show series of data or functions using a line chart.
Definition: LineChart.h:43
void setYData(string series, vector< double > ydata)
Changes the Y data for a series.
Definition: LineChart.h:234
void setXData(string series, vector< double > xdata)
Changes the X data for a series.
Definition: LineChart.h:214
Definition: Array.h:9
Support for drawing Bar charts.
Definition: alltypes.h:4