Performance cost of built-in functions returning single values.
Posted: Thu Mar 31, 2022 6:32 am
I'm once again stumbling into the forums as my own beliefs have been challenged by code examples in the wiki,
I'm wondering what the performance cost of some functions such as love.graphics.getWidth(), love.mouse.getX(), or love.timer.getTime() are.
When I first started working with Love2D, I assumed that I should save these into variables so that I don't call these functions repeatedly causing minuscule loss of processing cycles, and instead just sacrifice some memory for it. My current contrasting belief has been that I shouldn't add useless variables to my code and use minuscule amounts of extra memory, since these functions probably just provide variables from memory.
However, as I've been looking at the examples in the wiki, I see them save results from functions that provide a single variable, so I'm wondering what the actual performance impact of calling these is? Are some of such functions simply recalling variables from the framework, or do they all do calculations to provide the value? I assume calculation would be done for getTime() for example, but for the others I'm not so sure.
I'm aware that whatever the performance impact of these would be, it would in most cases the negligible, unless they get called so many times, but I'm looking for peace of mind regarding this.
TL;DR: Should I save the returns of functions such as love.graphics.getWidth() to variables if I'll use them multiple times?
I'm wondering what the performance cost of some functions such as love.graphics.getWidth(), love.mouse.getX(), or love.timer.getTime() are.
When I first started working with Love2D, I assumed that I should save these into variables so that I don't call these functions repeatedly causing minuscule loss of processing cycles, and instead just sacrifice some memory for it. My current contrasting belief has been that I shouldn't add useless variables to my code and use minuscule amounts of extra memory, since these functions probably just provide variables from memory.
However, as I've been looking at the examples in the wiki, I see them save results from functions that provide a single variable, so I'm wondering what the actual performance impact of calling these is? Are some of such functions simply recalling variables from the framework, or do they all do calculations to provide the value? I assume calculation would be done for getTime() for example, but for the others I'm not so sure.
I'm aware that whatever the performance impact of these would be, it would in most cases the negligible, unless they get called so many times, but I'm looking for peace of mind regarding this.
TL;DR: Should I save the returns of functions such as love.graphics.getWidth() to variables if I'll use them multiple times?