Added stopwatch

This commit is contained in:
MarcUs7i 2025-05-07 18:18:53 +02:00
parent 0516d85094
commit e25fbf7a3a
2 changed files with 70 additions and 2 deletions

View file

@ -35,7 +35,7 @@ typedef enum {
* @param algorithm The sorting algorithm
* @return The name of the algorithm.
*/
<type> get_algorithm_name(<params>);
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(<params>);
void sort_list(IntList list, SortingAlgorithm algorithm, criterion_fn criterion);
#else /* ARRAY_VARIANT */

View file

@ -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;
}