[Solved]How to measure the time it takes for a function to execute?

SadiQ

  • Posts: 1795
I would like to measure the time it takes for my pathfinding function to finish computing but I have no idea how to do that :(
In Stencyl\plaf\haxe\std\haxe\Timer.hx I saw the following:
Code: [Select]
/**
Measures the time it takes to execute [f], in seconds with fractions.

This is a convenience function for calculating the difference between
Timer.stamp() before and after the invocation of [f].

The difference is passed as argument to Log.trace(), with "s" appended
to denote the unit. The optional [pos] argument is passed through.

If [f] is null, the result is unspecified.
**/
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;
}
After importing the Timer class I did :
var t:Timer = new Timer(20);
t.measure(myPathfinder.findsomepath());
The result was this error: Cannot access static field measure from a class instance
Anybody out here that can share some info?

« Last Edit: August 28, 2014, 10:23:36 pm by SadiQ »
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

rob1221

  • *
  • Posts: 9473
Static functions are called for their class names, so it's Timer.measure.

SadiQ

  • Posts: 1795
Thanks rob for pointing that out, but after doingTimer.measure(myPathfinder.findsomepath()) I get an error about Void should be Void -> Unknown<0> for function argument 'f'.
Should my pathfinding function return a value or is that the wrong way to run that Timer.measure function?
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

rob1221

  • *
  • Posts: 9473
Try adding a Dynamic parameter to your callback function.  You might also need to leave out the parenthesis, but I'm not sure.

SadiQ

  • Posts: 1795
It was working if I used Timer.measure(myPathfinder.findSomePath) but not if I was using Timer.measure(myPathfinder.findSomePath(100)).
The solution was Timer.measure.myPathfinder.findSomePath.bind(100)
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.