Bridges-C++  3.4.1
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  } else {
83  int i;
84  for (i = 0; i < n-20; i++) {
85  arr[i] = i;
86  }
87  for (; i < n; i++) {
88  arr[i] = ((double)rand()) / RAND_MAX * (2 * n);
89  }
90  }
91  }
92 
93 
94  void generate(int* arr, int n) {
95  if (generatorType == "random") {
96  generateRandom(arr, n);
97  } else if (generatorType == "inorder") {
98  generateInOrder(arr, n);
99  } else if (generatorType == "reverseorder") {
100  generateReverseOrder(arr, n);
101  } else if (generatorType == "fewdifferentvalues") {
102  generateFewValues(arr, n);
103  } else if (generatorType == "almostsorted") {
104  generateAlmostSorted(arr, n);
105  } else {
106  throw std::string("unknown generator");
107  }
108 
109  }
110 
111  bool check (int* arr, int n) {
112  bool ok = true;
113  for (int i = 1; i < n; ++i) {
114  if (arr[i] < arr[i - 1]) {
115  ok = false;
116  }
117  }
118  return ok;
119  }
120 
121  public:
123  : plot (p) {
124  p.setXLabel("Size of Array");
125  p.setYLabel("Runtime (in s)");
126 
127  //r = new Random();
128 
129  maxSize = 1;
130  baseSize = 1;
131  increment = 1;
132  geoBase = 1.;
133  time_cap = std::numeric_limits<double>::max();
134  setGenerator("random");
135  }
136 
142  void setGenerator(const std::string& generatorName) {
143  generatorType = generatorName;
144  }
145 
146  std::string getGenerator() const {
147  return generatorType;
148  }
154  void setMaxSize(int size) {
155  maxSize = size;
156  }
157 
163  void setBaseSize(int size) {
164  baseSize = size;
165  }
166 
167 
173  void setIncrement(int inc) {
174  increment = inc;
175  }
176 
182  void setGeometric(double base) {
183  geoBase = base;
184  }
185 
197  void linearRange(int baseSize, int maxSize, int nbPoint) {
198  setBaseSize (baseSize);
199  setMaxSize (maxSize);
200  setIncrement ((maxSize - baseSize) / nbPoint);
201  setGeometric (1.0);
202  }
203 
216  void geometricRange(int baseSize, int maxSize, double base) {
217  setBaseSize (baseSize);
218  setMaxSize (maxSize);
219  setIncrement (0);
220  setGeometric (base);
221  if (base <= 1.0) {
222  std::cerr << "base should be > 1.0\n";
223  }
224  }
225 
235  void setTimeCap(double cap_in_s) {
236  time_cap = cap_in_s;
237  }
238 
245  void run(std::string algoName, void (*runnable)(int*, int)) {
246  std::vector<double> time;
247  std::vector<double> xData;
248 
249  // System.out.println(geoBase);
250  // System.out.println(increment);
251 
252  for (int n = baseSize; n <= maxSize;
253  n = std::max((int)(geoBase * n) + increment, n + 1)) {
254 
255  //System.out.println(n);
256  std::vector<int> arr(n);
257 
258  generate(&arr[0], n);
259 
260  std::chrono::time_point<std::chrono::system_clock> start = std::chrono::system_clock::now();
261 
262  runnable(&arr[0], n);
263 
264  std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();
265 
266  std::chrono::duration<double> elapsed_seconds = end - start;
267 
268  if (! check(&arr[0], n)) {
269  std::cerr << "Sorting algorithm " << algoName << " is incorrect\n";
270  }
271 
272  time.push_back ((double)elapsed_seconds.count() );
273  xData.push_back ( (double)n );
274 
275  if (elapsed_seconds.count() > time_cap) {
276  break;
277  }
278  }
279  plot.setXData(algoName, xData);
280  plot.setYData(algoName, time);
281  }
282 
283  };
284  }
285 }
286 
287 #endif
void setGenerator(const std::string &generatorName)
Definition: SortingBenchmark.h:142
void setTimeCap(double cap_in_s)
sets an upper bound to the time of a run.
Definition: SortingBenchmark.h:235
void setGeometric(double base)
Sets a geometric progression for the benchmark size.
Definition: SortingBenchmark.h:182
void geometricRange(int baseSize, int maxSize, double base)
The benchmark will sample a range using in geometrically increasing sequence.
Definition: SortingBenchmark.h:216
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:154
void setBaseSize(int size)
Smallest array to be used.
Definition: SortingBenchmark.h:163
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:245
void linearRange(int baseSize, int maxSize, int nbPoint)
The benchmark will sample a range with a fixed number of points.
Definition: SortingBenchmark.h:197
std::string getGenerator() const
Definition: SortingBenchmark.h:146
void setIncrement(int inc)
Sets the increment for the benchmark size.
Definition: SortingBenchmark.h:173
SortingBenchmark(LineChart &p)
Definition: SortingBenchmark.h:122
void setYData(string series, vector< double > ydata)
Changes the Y data for a series.
Definition: LineChart.h:237
Definition: Array.h:9