Funkin/source/funkin/util/TimerUtil.hx

50 lines
1.2 KiB
Haxe
Raw Normal View History

2024-03-13 21:47:15 -04:00
package funkin.util;
import funkin.util.tools.FloatTools;
import haxe.Timer;
2024-03-13 21:47:15 -04:00
class TimerUtil
{
2024-03-16 22:20:22 -04:00
/**
* Store the current time.
*/
public static function start():Float
{
return Timer.stamp();
}
2024-03-16 22:20:22 -04:00
/**
* Return the elapsed time.
*/
static function took(start:Float, ?end:Float):Float
{
var endOrNow:Float = end != null ? end : Timer.stamp();
return endOrNow - start;
}
2024-03-16 22:20:22 -04:00
/**
* Return the elapsed time in seconds as a string.
* @param start The start time.
* @param end The end time.
* @param precision The number of decimal places to round to.
* @return The elapsed time in seconds as a string.
*/
public static function seconds(start:Float, ?end:Float, ?precision = 2):String
{
var seconds:Float = FloatTools.round(took(start, end), precision);
return '${seconds} seconds';
}
2024-03-16 22:20:22 -04:00
/**
* Return the elapsed time in milliseconds as a string.
* @param start The start time.
* @param end The end time.
* @return The elapsed time in milliseconds as a string.
*/
public static function ms(start:Float, ?end:Float):String
{
var seconds:Float = took(start, end);
return '${seconds * 1000} ms';
}
}