[英]which one these is better for performance
I'm solving a problem and I need to optimize my code as much as possible. 我正在解决一个问题,我需要尽可能地优化我的代码。
I have a loop in which I need to do some calculation and I need to add an element to an array for further calculation: 我有一个循环,需要进行一些计算,并且需要向数组中添加元素以进行进一步的计算:
for (int i = 0; i < lightCount; i++) {
int dis = in.nextInt();
int dur = in.nextInt();
double unit = distance / duration * 3.6;
array[i] = unit;
if (speed < unit) {
double n = unit / (2 * speed);
if (!((n - (int) n) == 0))
n = n + 1;
int tmp = (int) (unit / (2 * (int) n));
if (tmp < answer)
answer = tmp;
}
}
Which is the best of the following situations: 在以下情况中,最好的是:
unit
or access the array directly (replacing unit
with array[i]
) unit
或直接访问数组(用array[i]
替换unit
) n
and replacing it with it's formula n
并将其替换为公式 This does not matter. 没关系。
This only matters if you call functions in your calculations and use the same expression multiple times. 这仅在您在计算中调用函数并多次使用同一表达式时才重要。 In this case, it does not matter.
在这种情况下,没关系。
This does not matter. 没关系。 For readability, you might want to split different functionality into different functions.
为了提高可读性,您可能需要将不同的功能拆分为不同的功能。
EDIT: IO is often the bottleneck. 编辑:IO通常是瓶颈。 What does in.nextInt() do?
in.nextInt()有什么作用? Can you move it outside of the loop?
您可以将其移出循环吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.