timer.h
A very simple library that encapsulates clock()
from <time.h>
, making life easier.
cmc_timer
struct cmc_timer
{
clock_t start;
clock_t stop;
double result;
};
clock_t start
- Start of timingclock_t stop
- End of timingdouble result
- Result in milliseconds
cmc_timer_start
Starts the timer by setting start
with clock()
.
#define cmc_timer_start(timer)
timer
- Astruct cmc_timer
variable
cmc_timer_stop
Sets the stop
variable with clock()
and calculates the result
in milliseconds.
#define cmc_timer_start(timer)
timer
- Astruct cmc_timer
variable
Example
#include <stdio.h>
#include "utl/timer.h"
int main(void)
{
struct cmc_timer t;
cmc_timer_start(t);
size_t total = 0;
for (size_t i = 0; i < 1000000000; i++)
total += i;
cmc_timer_stop(t);
printf("Sum took %.0lf milliseconds to calculate\n", t.result);
}