diff --git a/sorting.h b/sorting.h index d91ece3..95b6da5 100644 --- a/sorting.h +++ b/sorting.h @@ -35,7 +35,7 @@ typedef enum { * @param algorithm The sorting algorithm * @return The name of the algorithm. */ - get_algorithm_name(); +char* get_algorithm_name(SortingAlgorithm algorithm); #ifdef LIST_VARIANT @@ -64,7 +64,7 @@ void print_list(char* prefix, IntList list); * @param criterion The pointer to the function that implements the sorting criterion. * That function accepts two integer parameters and returns a boolean value. */ -void sort_list(); +void sort_list(IntList list, SortingAlgorithm algorithm, criterion_fn criterion); #else /* ARRAY_VARIANT */ diff --git a/stopwatch.c b/stopwatch.c index 3c587c3..10c19d3 100644 --- a/stopwatch.c +++ b/stopwatch.c @@ -25,3 +25,71 @@ * -> seconds = ticks / CLOCK_PER_SEC * microseconds are s * 10^-6 */ + +static clock_t start_time = 0; +static clock_t passed_time = 0; +static bool is_active = false; + +/** + * Starts taking the time. This function always starts at 0. + */ +void stopwatch_start() +{ + start_time = clock(); + passed_time = 0; + is_active = true; +} + +/** + * Stops or pauses taking the time. Time measurement can be resumed + * via `stopwatch_resume`. + */ +void stopwatch_stop() +{ + if (!is_active) { + return; + } + + passed_time += clock() - start_time; + is_active = false; +} + +/** + * Resumes taking the time. The previously measured time value is + * used as start value. + */ +void stopwatch_resume() +{ + if (is_active) { + return; + } + + start_time = clock(); + is_active = true; +} + +/** + * Determines whether or not the stopwatch takes the time. + * @return True if the stopwatch is measuring, false otherwise. + */ +bool stopwatch_is_active() +{ + return is_active; +} + +/** + * The measured time in microseconds. + * + * @return Either the processor time elapsed since start_stopwatch() has been called or + * the processor time elapsed between the calls of start_stopwatch() and stop_stopwatch(). + */ +double stopwatch_get_elapsed_time() +{ + clock_t total_time = passed_time; + + if (is_active) { + total_time += clock() - start_time; + } + + return ((double)total_time / CLOCKS_PER_SEC) * 1000000.0; +} \ No newline at end of file