Does using get functions every step decrease performance?

Checking get functions every step:
GML:
if (surface_get_width(shad_surf) != _cameraW) | (surface_get_height(shad_surf) != _cameraH) {}
Or creating two non local variables and checking/updating them every step:
GML:
if (cameraPrevW != _cameraW) | (cameraPrevH != _cameraH) {}
cameraPrevW = _cameraW; cameraPrevH = _cameraH;
Which one would be better for fps performance? Is it a waste of time to create these variables when I could just use get functions?

Thanks for any answers...
 
Generally, these are micro-optimisations that should be looked at last when doing optimisation, there's usually other things that are taking much more processing time (for instance, drawing a lot of text every frame instead of using surfaces, unnecessary/unoptimised loops and other things of that nature). That being said, my assumption would be that storing the values in variables and checking the variables would be faster than running the functions, however, the only way to really know would be to run a benchmark test.
 

Nidoking

Member
If you're using the values more than once per event/function, and they don't change in the meantime, I'd store the result of the get in a temporary variable. But the drawbacks of storing those values when there are getters anyway seem to outweigh the benefits. How often do they change? How will you know when they've changed without using the get functions you're trying to write around?
 

TheouAegis

Member
if (cameraPrevW != _cameraW) | (cameraPrevH != _cameraH) {}
cameraPrevW = _cameraW; cameraPrevH = _cameraH;
In this situation, any potential gain would be arguably negligible. That being said, on average a get_ function would be slower if the information doesn't need to be updated constantly, because the program has to jump to the function's subroutine, then look up the resource in question, before fetching the data associated with that resource. In your examples, the information is constantly updated, so you're likely only talking a gain of microseconds. But then it raises another issue: why don't you know these values already? The Fates didn't decide arbitrarily how big that surface will be, did they? Your program knew how big that surface was going to be at some point, so why doesn't it know now? If I give you 100 dimes and you never spend them, do you count out the dimes like Scrooge McDuck, making sure you still have 100 dimes from day to day?
 
How often do they change? How will you know when they've changed without using the get functions you're trying to write around?
I'm saving the cam's w/h in two variables and check if they are different then before, using only cam's get functions which I already needed to update in a local variable. If they are different I resize the surface to match the cam, so just resize when cam's size changed. I wonder if it was okay to just get surface's w/h and check if it's the same as camera's, and resize if it's not, since it would be more direct.

I could also not even check anything and just resize the surface every frame but I thought it would be too big of a power waste.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Any code that runs decreases performance. The better question is by how much.

That question is answered by the profiler. So if you're curious, run your game in debug mode and check it. Then change your code to something else and compare if it makes a difference, and if so, if that level of difference makes it worthwhile to optimize that kind of code in that kind of way. If you're shaving off mere microseconds, it probably isn't.
 

Nidoking

Member
I'm saving the cam's w/h in two variables
I reason this way: If you're using variables that you already maintain to set the camera parameters, then you already have the values of the camera parameters in variables, and you also know when those parameters will change, because you're changing them. Just do whatever else you need to do at that time. If your design is "Whenever I change variable a, variable b needs to update", then every time you set variable a, also set variable b accordingly. I do this by making it a function and using only that function to set the variable.
 
If your design is "Whenever I change variable a, variable b needs to update", then every time you set variable a, also set variable b accordingly. I do this by making it a function and using only that function to set the variable.
That makes sense, I guess changing another instance's variable once (which is what I need to do if I don't use these methods) is more efficient than checking another instance's variable every frame. I hadn't realized it, thanks!
 
Top