Reputation: 125
So I have a performance question. Is it better to declare more variables and then pass them as arguments or to calculate it when passing it as an argument for a function. I'll give an example:
shapeRenderer.rect(gameWidth/32 -(border/2) + gameWidth/60,gameHeight/18 - (border/2), gameWidth/3 + border ,gameHeight/30 + border);
Since this function is being called every frame, does that mean it's being calculated, for 50 fps, 50 times over and over again? Would it increase performance if I declared new variables in the constructor, like:
float x = gameWidth/32 -(border/2) + gameWidth/60;
float y = gameHeight/18 - (border/2);
float width = gameWidth/3 + border;
float height = gameHeight/30 + border;
and then called the function:
shapeRenderer.rect(x, y, width, height);
Since I have let's say 100 rectangles being drawn this would lead to 400 variables(every rectangle has different position and dimensions). Would that increase performance?
Upvotes: 4
Views: 89
Reputation: 579
If your rectangles have fixed dimensions/position than it would be better to pre calculate the values when initializing the rectangle, but if those value can change, you will have to recalculate everything.
Suggestion:
As long as you don't have a performance issue, it's not advisable to try fine tuning your application.
Upvotes: 2
Reputation: 1215
No, it wouldn't. If you look at the java ByteCode the instructions will be the same.
Upvotes: 0
Reputation: 811
Declaring new variables in the constructor should be faster. But it is also quite possible that due to the optimizations the JVM performs that you will feel no speed execution benefit .
Upvotes: 0