-
-
Save bradparks/8321942 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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