From 9e863a240c45df62d30c0ffcc5b5bc0d3b06451f Mon Sep 17 00:00:00 2001 From: "Kjell-Philipp A. Hahn" Date: Fri, 15 Apr 2022 19:05:02 +0200 Subject: [PATCH 1/3] feat: implement first version of bench command --- src/supersort.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/supersort.c b/src/supersort.c index 5d7a775..010196a 100644 --- a/src/supersort.c +++ b/src/supersort.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "algorithms.h" void print_usage_main() @@ -17,6 +18,12 @@ void print_usage_run() printf("valid algorithms are: insertion, selection, bubble\n\n"); } +void print_usage_bench() +{ + printf("usage: supersort bench\n\n"); + printf("applies various sort algorithms on random arrays with different lenghts\n\n"); +} + int run(char *name) { void (*sort)(int array[], int len); @@ -62,6 +69,23 @@ int run(char *name) return 0; } +void bench(void (*sort)(int array[], int length)) +{ + const int STEP = 1000; + int array[STEP * 10]; + + for (int i = STEP; i <= STEP * 10; i += STEP) + { + for (int j = 0; j < i; j++) + { + array[j] = rand(); + } + long int time = clock(); + sort(array, i); + printf("%10d %10.3f ms\n", i, (double)(clock() - time) / (CLOCKS_PER_SEC / 1000)); + } +} + int main(int argc, char *argv[]) { if (argc == 1 || strcmp(argv[1], "--help") == 0) @@ -84,6 +108,26 @@ int main(int argc, char *argv[]) exit(1); } } + else if (strcmp(argv[1], "bench") == 0) + { + if (argc != 2) + { + print_usage_bench(); + exit(1); + } + + printf("\nBUBBLE SORT\n"); + printf("%10s %10s\n", "elements", "speed"); + bench(bubble_sort); + + printf("\nSELECTION SORT\n"); + printf("%10s %10s\n", "elements", "speed"); + bench(selection_sort); + + printf("\nINSERTIONS SORT\n"); + printf("%10s %10s\n", "elements", "speed"); + bench(insertion_sort); + } else { printf("supersort: '%s' is not a supersort command. See 'supersort --help'.\n", argv[1]); From 22ae3912738e8033dd7e5d1203ec187b3a9f04eb Mon Sep 17 00:00:00 2001 From: "Kjell-Philipp A. Hahn" Date: Fri, 15 Apr 2022 19:20:59 +0200 Subject: [PATCH 2/3] style: all local variables to lowercase --- src/supersort.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/supersort.c b/src/supersort.c index 010196a..fb1f38d 100644 --- a/src/supersort.c +++ b/src/supersort.c @@ -71,10 +71,10 @@ int run(char *name) void bench(void (*sort)(int array[], int length)) { - const int STEP = 1000; - int array[STEP * 10]; + const int step = 1000; + int array[step * 10]; - for (int i = STEP; i <= STEP * 10; i += STEP) + for (int i = step; i <= step * 10; i += step) { for (int j = 0; j < i; j++) { From 72d7b2c56613701ba3c1553d8d29e4abc3a5850a Mon Sep 17 00:00:00 2001 From: "Kjell-Philipp A. Hahn" Date: Mon, 18 Apr 2022 15:53:14 +0200 Subject: [PATCH 3/3] refactor: relocated print statement to make code more DRY --- src/supersort.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/supersort.c b/src/supersort.c index fb1f38d..f77761e 100644 --- a/src/supersort.c +++ b/src/supersort.c @@ -74,6 +74,7 @@ void bench(void (*sort)(int array[], int length)) const int step = 1000; int array[step * 10]; + printf("%10s %10s\n", "elements", "speed"); for (int i = step; i <= step * 10; i += step) { for (int j = 0; j < i; j++) @@ -117,15 +118,12 @@ int main(int argc, char *argv[]) } printf("\nBUBBLE SORT\n"); - printf("%10s %10s\n", "elements", "speed"); bench(bubble_sort); printf("\nSELECTION SORT\n"); - printf("%10s %10s\n", "elements", "speed"); bench(selection_sort); printf("\nINSERTIONS SORT\n"); - printf("%10s %10s\n", "elements", "speed"); bench(insertion_sort); } else