StopWatch

a simple stopwatch to measure how long something takes.

Usage


var s:StopWatch = new StopWatch("comma formatting took ");
Utils.commaFormat(123123123123123123123);
s.stop();
trace(s);

// is the same as

var s:StopWatch = new StopWatch("comma formatting took ");
Utils.commaFormat(123123123123123123123);
trace(s.stop());

// both output

comma formatting took 0.079 ms

Source


StopWatch.hx


import haxe.Timer;
class StopWatch{
    public var ms: Float;
    public var seconds(getSeconds,null):Float;
    private var timer: Timer;
    private var startTime:Int;
    private var preText:String;
    private var lastStamp:Float;
    
    public inline function new(preText:String='')
    {
        timer = new Timer(1);
        timer.run();
        
        this.preText = preText;
        lastStamp = 0;
    }
    
    public inline function stop():String
    {
        timer.stop();
        ms = getStamp();
        return(preText+" "+Std.string(ms)+" ms.");
    }
    
    private inline function getSeconds(  ):Float
    {
        return ms/1000;
    }
    
    private inline function getStamp():Float{
        var s:Float = Timer.stamp() - lastStamp;
        lastStamp = Timer.stamp();
        return s;
    }
    
    public inline function toString():String
    {
        return(preText+" "+Std.string(ms)+" ms.");
    }
}
version #6840, modified 2009-08-23 15:13:05 by theRemix