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);
84 for (i = 0; i < n-20; i++) {
88 arr[i] = ((double)rand()) / RAND_MAX * (2 * n);
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);
106 throw std::string(
"unknown generator");
111 bool check (
int* arr,
int n) {
113 for (
int i = 1; i < n; ++i) {
114 if (arr[i] < arr[i - 1]) {
124 p.setXLabel(
"Size of Array");
125 p.setYLabel(
"Runtime (in s)");
133 time_cap = std::numeric_limits<double>::max();
134 setGenerator(
"random");
143 generatorType = generatorName;
147 return generatorType;
198 setBaseSize (baseSize);
199 setMaxSize (maxSize);
200 setIncrement ((maxSize - baseSize) / nbPoint);
217 setBaseSize (baseSize);
218 setMaxSize (maxSize);
222 std::cerr <<
"base should be > 1.0\n";
245 void run(std::string algoName,
void (*runnable)(
int*,
int)) {
246 std::vector<double> time;
247 std::vector<double> xData;
252 for (
int n = baseSize; n <= maxSize;
253 n = std::max((
int)(geoBase * n) + increment, n + 1)) {
256 std::vector<int> arr(n);
258 generate(&arr[0], n);
260 std::chrono::time_point<std::chrono::system_clock> start = std::chrono::system_clock::now();
262 runnable(&arr[0], n);
264 std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();
266 std::chrono::duration<double> elapsed_seconds = end - start;
268 if (! check(&arr[0], n)) {
269 std::cerr <<
"Sorting algorithm " << algoName <<
" is incorrect\n";
272 time.push_back ((
double)elapsed_seconds.count() );
273 xData.push_back ( (
double)n );
275 if (elapsed_seconds.count() > time_cap) {
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