1 #ifndef SORTINGBENCHMARK_H
2 #define SORTINGBENCHMARK_H
56 std::string generatorType;
58 void generateRandom(
int* arr,
int n) {
59 for (
int i = 0; i < n; i++) {
60 arr[i] = ((double)rand()) / RAND_MAX * (2 * n);
64 void generateInOrder(
int* arr,
int n) {
65 for (
int i = 0; i < n; i++) {
69 void generateReverseOrder(
int* arr,
int n) {
70 for (
int i = 0; i < n; i++) {
74 void generateFewValues(
int* arr,
int n) {
75 for (
int i = 0; i < n; i++) {
76 arr[i] = ((double)rand()) / RAND_MAX * (4);
79 void generateAlmostSorted(
int* arr,
int n) {
81 generateRandom(arr, n);
85 for (i = 0; i < n - 20; i++) {
89 arr[i] = ((double)rand()) / RAND_MAX * (2 * n);
94 void generate(
int* arr,
int n) {
95 if (generatorType ==
"random") {
96 generateRandom(arr, n);
98 else if (generatorType ==
"inorder") {
99 generateInOrder(arr, n);
101 else if (generatorType ==
"reverseorder") {
102 generateReverseOrder(arr, n);
104 else if (generatorType ==
"fewdifferentvalues") {
105 generateFewValues(arr, n);
107 else if (generatorType ==
"almostsorted") {
108 generateAlmostSorted(arr, n);
111 throw std::string(
"unknown generator");
116 bool check (
int* arr,
int n) {
118 for (
int i = 1; i < n; ++i) {
119 if (arr[i] < arr[i - 1]) {
129 p.setXLabel(
"Size of Array");
130 p.setYLabel(
"Runtime (in s)");
138 time_cap = std::numeric_limits<double>::max();
139 setGenerator(
"random");
148 generatorType = generatorName;
152 return generatorType;
202 setBaseSize (baseSize);
203 setMaxSize (maxSize);
204 setIncrement ((maxSize - baseSize) / nbPoint);
221 setBaseSize (baseSize);
222 setMaxSize (maxSize);
226 std::cerr <<
"base should be > 1.0\n";
249 void run(std::string algoName,
void (*runnable)(
int*,
int)) {
250 std::vector<double> time;
251 std::vector<double> xData;
256 for (
int n = baseSize; n <= maxSize;
257 n = std::max((
int)(geoBase * n) + increment, n + 1)) {
260 std::vector<int> arr(n);
262 generate(&arr[0], n);
264 std::chrono::time_point<std::chrono::system_clock> start = std::chrono::system_clock::now();
266 runnable(&arr[0], n);
268 std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();
270 std::chrono::duration<double> elapsed_seconds = end - start;
272 if (! check(&arr[0], n)) {
273 std::cerr <<
"Sorting algorithm " << algoName <<
" is incorrect\n";
276 time.push_back ((
double)elapsed_seconds.count() );
277 xData.push_back ( (
double)n );
279 if (elapsed_seconds.count() > time_cap) {
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
Support for drawing Bar charts.
Definition: alltypes.h:4