user1949350
user1949350

Reputation: 125

Create more variables or calculate when passing arguments to function

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

Answers (3)

Illyes Istvan
Illyes Istvan

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

Preston Garno
Preston Garno

Reputation: 1215

No, it wouldn't. If you look at the java ByteCode the instructions will be the same.

Upvotes: 0

someone_somewhere
someone_somewhere

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

Related Questions