创建更多变量或在将参数传递给函数时进行计算
Create more variables or calculate when passing arguments to function
所以我有一个性能问题。是声明更多变量然后将它们作为参数传递还是在将其作为函数参数传递时计算它更好?我举个例子:
shapeRenderer.rect(gameWidth/32 -(border/2) + gameWidth/60,gameHeight/18 - (border/2), gameWidth/3 + border ,gameHeight/30 + border);
既然这个函数每帧都被调用,这是否意味着它被计算,50 帧/秒,一遍又一遍地计算 50 次?如果我在构造函数中声明新变量,它会提高性能吗,比如:
float x = gameWidth/32 -(border/2) + gameWidth/60;
float y = gameHeight/18 - (border/2);
float width = gameWidth/3 + border;
float height = gameHeight/30 + border;
然后调用函数:
shapeRenderer.rect(x, y, width, height);
因为我假设要绘制 100 个矩形,这将导致 400 个变量(每个矩形都有不同的位置和尺寸)。这会提高性能吗?
在构造函数中声明新变量应该会更快。但也很有可能由于 JVM 执行的优化,您将感觉不到执行速度的好处。
不,不会。如果您查看 java 字节码,指令将是相同的。
如果你的矩形已经固定 dimensions/position ,那么在初始化矩形时预先计算值会更好,但如果这些值可以改变,你将不得不重新计算所有内容。
建议:
As long as you don't have a performance issue, it's not advisable to
try fine tuning your application.
所以我有一个性能问题。是声明更多变量然后将它们作为参数传递还是在将其作为函数参数传递时计算它更好?我举个例子:
shapeRenderer.rect(gameWidth/32 -(border/2) + gameWidth/60,gameHeight/18 - (border/2), gameWidth/3 + border ,gameHeight/30 + border);
既然这个函数每帧都被调用,这是否意味着它被计算,50 帧/秒,一遍又一遍地计算 50 次?如果我在构造函数中声明新变量,它会提高性能吗,比如:
float x = gameWidth/32 -(border/2) + gameWidth/60;
float y = gameHeight/18 - (border/2);
float width = gameWidth/3 + border;
float height = gameHeight/30 + border;
然后调用函数:
shapeRenderer.rect(x, y, width, height);
因为我假设要绘制 100 个矩形,这将导致 400 个变量(每个矩形都有不同的位置和尺寸)。这会提高性能吗?
在构造函数中声明新变量应该会更快。但也很有可能由于 JVM 执行的优化,您将感觉不到执行速度的好处。
不,不会。如果您查看 java 字节码,指令将是相同的。
如果你的矩形已经固定 dimensions/position ,那么在初始化矩形时预先计算值会更好,但如果这些值可以改变,你将不得不重新计算所有内容。
建议:
As long as you don't have a performance issue, it's not advisable to try fine tuning your application.