gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Nasty slowdown from 3 lines of code..

#44273 - Ultima2876 - Tue May 31, 2005 4:30 pm

Code:
source_variables = (spriteno * 16);
source_colltype = (obj_var[(source_variables + 0)] - ((obj_var[(source_variables + 0)] >> 8) << 8));
source_ysize = (obj_var[(source_variables + 5)] - ((obj_var[(source_variables + 5)] >> 8) << 8));


My game's got an FPS counter in it, and when I comment these three lines out I gain 8 FPS. For three lines of code, I think that's just wrong.

source_variables is an int, obj_var[] is an array of signed ints (in Iwram - it's 2048 elements), and spriteno is an int. The odd shifting at the end of the lines is to get rid of some bits (the wole calculation is to get just the lower 8 bits, because the upper bits contain data for another variable). I didn't think this could possibly be the source of slowdown, since the shifts are fast... anyone have a possible explanation?

Cheers.

#44276 - strager - Tue May 31, 2005 4:57 pm

Well, it is MUCH easier to use an AND then a SHIFT:
Code:

source_variables = spriteno * 16;
source_colltype = obj_var[source_variables] & 0xFF;
source_ysize = obj_var[source_variables + 5] & 0xFF;


Try that and see if there is an improvement.

#44277 - poslundc - Tue May 31, 2005 5:16 pm

Assert to make sure you aren't exceeding the bounds of your array and accessing invalid areas of memory; that could have something to do with it.

Dan.

#44284 - Ultima2876 - Tue May 31, 2005 5:35 pm

It worked, thanks - that section run nicely now ^_^