Tidyng and optimising the timer class a bit

This commit is contained in:
underscorediscovery
2013-09-14 02:07:51 -02:30
parent 5943b89246
commit 381772913b

View File

@@ -1,18 +1,122 @@
package haxe; package haxe;
#if (macro || (!neko && !cpp))
#if lime_native
class Timer {
static var sRunningTimers:Array<Timer> = [];
@:noCompletion public var time:Float;
@:noCompletion public var fire_at:Float;
@:noCompletion public var running:Bool;
public function new(_time:Float) {
time = _time;
sRunningTimers.push (this);
fire_at = GetMS () + time;
running = true;
}
public static function measure<T>( f : Void -> T, ?pos : PosInfos ) : T {
var t0 = stamp();
var r = f();
Log.trace((stamp() - t0) + "s", pos);
return r;
}
// Set this with "run=..."
dynamic public function run () { }
public function stop ():Void {
if (running) {
running = false;
sRunningTimers.remove (this);
}
}
// Original haxe.Timer class /**
* @private
*/
@:noCompletion static public function __nextWake( limit:Float ):Float {
class Timer { var now = nme_time_stamp () * 1000.0;
#if (neko || php || cpp) var sleep;
#else
for (timer in sRunningTimers) {
sleep = timer.fire_at - now;
if (sleep < limit) {
limit = sleep;
if (limit < 0) {
return 0;
}
}
} //for each timer
return limit * 0.001;
} //__nextWake
/**
* @private
*/
@:noCompletion public static function __checkTimers () {
var now = GetMS ();
for (timer in sRunningTimers) {
if(timer.running) {
if(now >= timer.fire_at) {
timer.fire_at += timer.time;
timer.run();
} //now
}
} //all timers
}
static function GetMS ():Float {
return stamp () * 1000.0;
}
// From std/haxe/Timer.hx
public static function delay( _f:Void -> Void, _time:Int ) {
var t = new Timer (_time);
t.run = function() {
t.stop();
_f();
};
return t;
} //delay
static public function stamp ():Float {
return nme_time_stamp ();
}
static var nme_time_stamp = lime.utils.Libs.load ("nme","nme_time_stamp", 0);
} //Timer
#else //lime_native
class Timer {
private var id : Null<Int>; private var id : Null<Int>;
/** // Create a new timer that will run every [time_ms] (in milliseconds).
Create a new timer that will run every [time_ms] (in milliseconds).
**/
public function new( time_ms : Int ){ public function new( time_ms : Int ){
#if flash9 #if flash9
var me = this; var me = this;
@@ -26,9 +130,7 @@ class Timer {
#end #end
} }
/** // Stop the timer definitely.
Stop the timer definitely.
**/
public function stop() { public function stop() {
if( id == null ) if( id == null )
return; return;
@@ -42,15 +144,12 @@ class Timer {
id = null; id = null;
} }
/** // This is the [run()] method that is called when the Timer executes. It can be either overriden in subclasses or directly rebinded with another function-value.
This is the [run()] method that is called when the Timer executes. It can be either overriden in subclasses or directly rebinded with another function-value.
**/
public dynamic function run() { public dynamic function run() {
} }
/**
This will delay the call to [f] for the given time. [f] will only be called once. // This will delay the call to [f] for the given time. [f] will only be called once.
**/
public static function delay( f : Void -> Void, time_ms : Int ) { public static function delay( f : Void -> Void, time_ms : Int ) {
var t = new haxe.Timer(time_ms); var t = new haxe.Timer(time_ms);
t.run = function() { t.run = function() {
@@ -60,11 +159,8 @@ class Timer {
return t; return t;
} }
#end
/** //Measure the time it takes to execute the function [f] and trace it. Returns the value returned by [f].
Measure the time it takes to execute the function [f] and trace it. Returns the value returned by [f].
**/
public static function measure<T>( f : Void -> T, ?pos : PosInfos ) : T { public static function measure<T>( f : Void -> T, ?pos : PosInfos ) : T {
var t0 = stamp(); var t0 = stamp();
var r = f(); var r = f();
@@ -72,9 +168,7 @@ class Timer {
return r; return r;
} }
/** // Returns the most precise timestamp, in seconds. The value itself might differ depending on platforms, only differences between two values make sense.
Returns the most precise timestamp, in seconds. The value itself might differ depending on platforms, only differences between two values make sense.
**/
public static function stamp() : Float { public static function stamp() : Float {
#if flash #if flash
return flash.Lib.getTimer() / 1000; return flash.Lib.getTimer() / 1000;
@@ -87,157 +181,8 @@ class Timer {
#else #else
return 0; return 0;
#end #end
} } //stamp
} } //Timer
#else
#end //!native
// Custom haxe.Timer implementation for C++ and Neko
typedef TimerList = Array <Timer>;
class Timer {
static var sRunningTimers:TimerList = [];
var mTime:Float;
var mFireAt:Float;
var mRunning:Bool;
public function new (time:Float) {
mTime = time;
sRunningTimers.push (this);
mFireAt = GetMS () + mTime;
mRunning = true;
}
public static function measure<T>( f : Void -> T, ?pos : PosInfos ) : T {
var t0 = stamp();
var r = f();
Log.trace((stamp() - t0) + "s", pos);
return r;
}
// Set this with "run=..."
dynamic public function run () {
}
public function stop ():Void {
if (mRunning) {
mRunning = false;
sRunningTimers.remove (this);
}
}
/**
* @private
*/
@:noCompletion static public function __nextWake (limit:Float):Float {
var now = nme_time_stamp () * 1000.0;
var sleep;
for (timer in sRunningTimers) {
sleep = timer.mFireAt - now;
if (sleep < limit) {
limit = sleep;
if (limit < 0) {
return 0;
}
}
}
return limit * 0.001;
}
@:noCompletion function __check (inTime:Float) {
if (inTime >= mFireAt) {
mFireAt += mTime;
run ();
}
}
/**
* @private
*/
@:noCompletion public static function __checkTimers () {
var now = GetMS ();
for (timer in sRunningTimers) {
timer.__check (now);
}
}
static function GetMS ():Float {
return stamp () * 1000.0;
}
// From std/haxe/Timer.hx
public static function delay (f:Void -> Void, time:Int) {
var t = new Timer (time);
t.run = function () {
t.stop ();
f ();
};
return t;
}
static public function stamp ():Float {
return nme_time_stamp ();
}
static var nme_time_stamp = lime.utils.Libs.load ("nme","nme_time_stamp", 0);
}
#end