MSUTimer
v0.01 alpha
Simple High Resolution Timer for C
|
Simple High Resolution Timer for C (public interface). More...
#include <stddef.h>
#include <stdbool.h>
Go to the source code of this file.
Macros | |
Convenience Macros | |
Benchmark functions (and others) return microseconds, so the following macros may prove handy to the caller. | |
#define | MSUT_US2MS(usecs) ((usecs) * 0.001) |
Convert microseconds to milliseconds. | |
#define | MSUT_US2S(usecs) ((usecs) * 0.000001) |
Convert microseconds to seconds. | |
Typedefs | |
typedef struct MSUTimer_ | MSUTimer |
Opaque type (forward-declaration) | |
Functions | |
MSUTimer * | msutimer_new (void) |
Create a new timer. More... | |
MSUTimer * | msutimer_free (MSUTimer *timer) |
Free an existing timer. More... | |
double | msutimer_accuracy_usecs (MSUTimer *timer) |
Get MSUTimer accuracy. More... | |
double | msutimer_gettime (MSUTimer *timer) |
Get current time & update time-difference. More... | |
double | msutimer_diff_usecs (MSUTimer *timer) |
Get updated time-difference in microseconds. More... | |
double | msutimer_diff_msecs (MSUTimer *timer) |
Get updated time-difference in milliseconds. More... | |
double | msutimer_diff_secs (MSUTimer *timer) |
Get updated time-difference in seconds. More... | |
double | msutimer_bench (MSUTimer *timer, size_t nrepeats, bool(*callback)(void *), void *userdata, size_t *erepeat) |
Get callback's execution time. More... | |
double | msutimer_bench_average (MSUTimer *timer, size_t nrepeats, bool(*callback)(void *), void *userdata, size_t *erepeat) |
Get callback's average execution time. More... | |
double | msutimer_bench_median (MSUTimer *timer, size_t nrepeats, bool(*callback)(void *), void *userdata, size_t *erepeat) |
Get callback's median execution time. More... | |
Simple High Resolution Timer for C (public interface).
double msutimer_accuracy_usecs | ( | MSUTimer * | timer | ) |
Get MSUTimer accuracy.
Obtains the minimum time-interval that can be measured by MSUTimer, on the running platform. The returned microseconds can be converted to milliseconds or seconds, with the convenience macros MSUT_US2MS() or MSUT_US2S(), respectively.
timer | Any already created timer. |
double
representing the MSUTimer accuracy on the current platform in microseconds, or -DBL_MAX
on error. NULL
(errno
is set to EDOM
) double msutimer_bench | ( | MSUTimer * | timer, |
size_t | nrepeats, | ||
bool(*)(void *) | callback, | ||
void * | userdata, | ||
size_t * | erepeat | ||
) |
Get callback's execution time.
Measures the execution time of its callback-function argument, after a specified number of iterations.
timer | An already created timer. |
nrepeats | The number of times to execute the callback-function (iterations). |
callback | The callback-function to be measured. It should be of type bool (C99) returning false on error, true otherwise. Its prototype should be the following: bool (*callback)(void *userdata);
|
userdata | A void pointer to caller-defined data, used by the callback-function. This pointer gets directly passed to the callback-function without any processing. It may be NULL if the callback-functions doesn't use any extra data. |
erepeat | If non-NULL , then in case of callback error (false ) erepeat passes back to the caller the last successful iteration. |
double
representing the time spent (in microseconds) to execute the callback-function nrepeats
times.If the callback-function errors before completing nrepeats
iterations, then the returned value is negative and erepeat
(if non-NULL
) passes back to the caller the last successful iteration. Hence a negative return value reflects the number of microseconds elpased until the erepeat
'th iteration.
On all other errors, errno
is set to EDOM
and the function returns 0.0
errno
against EDOM
(or not being 0). In MSUTimer debug mode (e.g. compiled with -DMSUTDEBUG
or -DMSUTDEBUG=2
) errors are auto reported to stderr
.NULL
(returns 0.0, errno
is set to EDOM
)errno
is set to EDOM
)NULL
(returns 0.0, errno
is set to EDOM
)false
(erepeat
is set to the last successful iteration and the function returns the elapsed microseconds until then, as a negative double
). double msutimer_bench_average | ( | MSUTimer * | timer, |
size_t | nrepeats, | ||
bool(*)(void *) | callback, | ||
void * | userdata, | ||
size_t * | erepeat | ||
) |
Get callback's average execution time.
Measures the average execution time of its callback-function argument, after a specified number of iterations.
nrepeats
iterations, instead of summing up all iterations.Put otherwise, msutimer_bench() returns the total time after nrepeats
iterations, while msutimer_bench_average() returns the average time of a single call after nrepeats
iterations.
Compared to msutimer_bench_median(), this function accounts excessive spikes in the computation (average vs. median).
double msutimer_bench_median | ( | MSUTimer * | timer, |
size_t | nrepeats, | ||
bool(*)(void *) | callback, | ||
void * | userdata, | ||
size_t * | erepeat | ||
) |
Get callback's median execution time.
Measures the median execution time of its callback-function argument, after a specified number of iterations.
nrepeats
iterations, instead of summing up all iterations.Put otherwise, msutimer_bench() returns the total time after nrepeats
iterations, while msutimer_bench_median() returns the median time of a single call after nrepeats
iterations.
Compared to msutimer_bench_average(), this function ignores excessive spikes in the computation (average vs. median).
double msutimer_diff_msecs | ( | MSUTimer * | timer | ) |
Get updated time-difference in milliseconds.
Queries its timer argument for the updated time-difference, after a call to msutimer_gettime(). Calling this function before calling msutimer_gettime() first, results to a random value (bogus time-difference).
timer | The timer to be queried. |
double
reflecting the updated time-difference in milliseconds after a call to msutimer_gettime(), or -DBL_MAX
on error. NULL
(errno
is set to EDOM
) double msutimer_diff_secs | ( | MSUTimer * | timer | ) |
Get updated time-difference in seconds.
Queries its timer argument for the updated time-difference, after a call to msutimer_gettime(). Calling this function before calling msutimer_gettime() first, results to a random value (bogus time-difference).
timer | The timer to be queried. |
double
reflecting the updated time-difference in seconds after a call to msutimer_gettime(), or -DBL_MAX
on error. NULL
(errno
is set to EDOM
) double msutimer_diff_usecs | ( | MSUTimer * | timer | ) |
Get updated time-difference in microseconds.
Queries its timer argument for the updated time-difference, after a call to msutimer_gettime(). Calling this function before calling msutimer_gettime() first, results to a random value (bogus time-difference).
timer | The timer to be queried. |
double
reflecting the updated time-difference in microseconds after a call to msutimer_gettime(), or -DBL_MAX
on error. NULL
(errno
is set to EDOM
) Free an existing timer.
De-allocates the memory reserved for its timer argument.
timer | The timer to be freed. |
NULL
, so the caller can opt to assign it back to the freed pointer, to avoid leaving it in a dangling state.double msutimer_gettime | ( | MSUTimer * | timer | ) |
Get current time & update time-difference.
Gets the current time and Updates its timer argument with the elapsed time since the previous call of the function, or the construction of the timer (whichever came last). The time-difference can then get fetched as a double
with one of the functions: msutimer_diff_usecs(), msutimer_diff_msecs(), msutimer_diff_secs(), which return microseconds, millseconds or seconds respectively.
Alternatively, the caller may opt to calculate the elapsed time manually, by storing and then subtracting the double
values returned by 2 calls of the function (similar to how the standard C function clock()
is used). This also allows for time measurements over multiple calls of the function. The result of the subtraction represents microseconds, which can then get passed to the convenience macros MSUT_US2MS() or MSUT_US2S() for converting them to milliseconds or seconds, respectively.
timer | The timer to be updated. |
double
, representing the current time in microseconds. Useful for calculating manually the time-difference between 2 calls of the function. NULL
(errno
is set to EDOM
) MSUTimer* msutimer_new | ( | void | ) |
Create a new timer.
Constructs, initializes and starts a new timer. De-allocation should be done by the caller, with msutimer_free().
NULL
on error.errno
is set by the C runtime)errno
is set to ERANGE
)