diff --git a/src/supersort.c b/src/supersort.c index 5d7a775..f77761e 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,24 @@ int run(char *name) return 0; } +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++) + { + 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 +109,23 @@ 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"); + bench(bubble_sort); + + printf("\nSELECTION SORT\n"); + bench(selection_sort); + + printf("\nINSERTIONS SORT\n"); + bench(insertion_sort); + } else { printf("supersort: '%s' is not a supersort command. See 'supersort --help'.\n", argv[1]);