diff --git a/src/clocks.c b/src/clocks.c index d5f0e571fe0a4694c4088bb05014fafa99d60488..c70788e0e17452e23944d523be25842b5155ec7c 100644 --- a/src/clocks.c +++ b/src/clocks.c @@ -216,6 +216,25 @@ double clocks_from_ticks(ticks tics) { return ((double)tics / (double)clocks_get_cpufreq() * clocks_units_scale); } +/** + * @brief Convert a number of milli seconds into ticks, if possible. + * + * Only an approximation as based on how well we have estimated the + * rtc frequency. Should be good for machines that support constant_rtc + * and clock_gettime(), and reasonable for most Linux machines, otherwise + * a guess will just be returned. See clocks_getunit() for the actual units. + * + * @param ms a number of "milliseconds" to convert to ticks + * + * @result the number of ticks, if possible. + */ +ticks clocks_to_ticks(double ms) { + return (ticks) (ms * (double)clocks_get_cpufreq() / clocks_units_scale); +} + +ticks clocks_to_ticks(double interval); + + /** * @brief return the time units. * diff --git a/src/clocks.h b/src/clocks.h index 4c69e130d9bfc5e61fb03fc9820ffc76d2440dc4..bdb3a6651e52f5b165e644015b91f96aa5812d57 100644 --- a/src/clocks.h +++ b/src/clocks.h @@ -39,6 +39,7 @@ const char *clocks_getunit(); void clocks_set_cpufreq(unsigned long long freq); unsigned long long clocks_get_cpufreq(); double clocks_from_ticks(ticks tics); +ticks clocks_to_ticks(double interval); double clocks_diff_ticks(ticks tic, ticks toc); const char *clocks_get_timesincestart();