Is there a way to time code? My A* is too slow.

N

Nitex

Guest
Hi guys, so I've implemented A* for my hex grid, but it's too slow, as in, it's causing noticeable lag. Note that each run of it isn't terribly slow, but I have to run it across ds_lists that can get up to a hundred entries long (using it as a pathfinding check).

If this were python, I'd throw some timing code on there and see what parts are the slowest. I thought delta time might be useful for this purpose, but it turns out not to be, I don't think, or maybe I'm just using it wrong. (All I do is print the delta time in a few places, all it says is stuff like "33347".)

Does anyone have a clever or generic way to time their code?
 

chamaeleon

Member
Or use something that actually lets you represent time elapsed between two points in your code, like get_timer(), unlike delta_time which gives you the time since the last step finished (which will not change for the entirety of the current step).
GML:
var t1 = get_timer();
/// expensive computation
var t2 = get_timer();

var t = (t2 - t1) / 1000000;

show_debug_message("Time: " + string(t));
 
N

Nitex

Guest
Awesome suggestions, thanks guys! I'll try both these out.

EDIT:
After trying this out, I think I know the problem. I found that each run only costs like 20 nanoseconds, but that 20 nanosecond loop is being run waaaaay too many times for some reason. Now to figure out why...
 
Last edited by a moderator:
I use a globalvar that switches back and forth based on a timer, it can then be read to define what codes to run but you need to use statements to condition things.

Example:

//Create Event
globalvar pworldimageset,pworldimage,pworldimagenow

pworldimage=0 //this is the switch.
pworldimageset=16 //these are the number of steps to occur, this also is used to define the pworldimageclock reset value when it reaches zero.
pworldimagenow=pworldimageset //sets the clock to it's starting value.

//Step
if pworldimagenow>0 {pworldimagenow-=1} else pworldimagenow=pworldimageset;
if pworldimage=0 {pworldimage=1} else pworldimage=0}; //This causes the switch to change value
 
Top