Skip to content

Instantly share code, notes, and snippets.

@bradparks
Forked from back2dos/DevTools.hx
Created January 8, 2014 18:36
Show Gist options
  • Select an option

  • Save bradparks/8321942 to your computer and use it in GitHub Desktop.

Select an option

Save bradparks/8321942 to your computer and use it in GitHub Desktop.
package ;
class DevTools {
macro static public function measure(e, ?msg:String) {
if (msg == null)
msg = haxe.macro.ExprTools.toString(e);
return macro @:pos(e.pos) {
var start = haxe.Timer.stamp(),
result = [$e];//wrapping in the array skews the result a bit, but avoids issues with Void
var duration = haxe.Timer.stamp() - start;
trace($v{msg}+ ' took '+duration + ' seconds');//one could add some formatting here
result[0];
}
}
}
package ;
import DevTools.*;
class Example {
static function guessPi(?samples = 1000000) {
var hits = 0;
for (i in 0...samples) {
var x = Math.random(),
y = Math.random();
if (x * x + y * y <= 1) hits++;
}
return hits / samples * 4;
}
static function main() {
measure(trace('foo'));
measure(for (i in 0...100000) {}, 'long loop');
trace('PI is approximately '+measure(guessPi()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment