Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relocated print statement into bench function #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/supersort.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "algorithms.h"

void print_usage_main()
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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]);
Expand Down