使用空体和不使用空体的for循环所花费的时间相同。

huangapple go评论70阅读模式
英文:

For-loop with and without an empty body takes same amount of time

问题

我注意到以下两个循环所花费的时间相同:约为 1.2 秒。

double count = Math.pow(10, 9);
for (int i = 0; i < count; i++) {
    int y = 10;
    int x = 3 * y;
    x = 100 * y;
    int[] arr = new int[3];
    arr[0] = 1;
}
double count = Math.pow(10, 9);
for (int i = 0; i < count; i++) {
    // 无操作
}

为什么它们会在相同的时间内完成?这些 for 循环只进行递增和 1 次比较,因此每次迭代有 2 个动作。我原以为第一个循环的完成时间会大约是第二个循环的 3 倍,因为第一个循环执行了 7 个动作,而第二个循环只执行了 2 个动作。

英文:

I've noticed that both of the following loops take the same amount of time: ~ 1.2 seconds.

double count = Math.pow(10, 9);
for (int i = 0; i &lt; count; i++) {
	int y = 10;
	int x = 3*y;
	x = 100*y;
	int[] arr = new int[3];
	arr[0] = 1;
}
double count = Math.pow(10, 9);
for (int i = 0; i &lt; count; i++) {
	//nothing
}

Why would they finish in the same time? These for-loops only increment and do 1 comparison, so 2 actions per iteration. I would think the first one would take about 3 times longer to finish, since it does 7 actions whereas the second one does 2.

答案1

得分: 2

答案不是因为优化(或仅仅是优化 - 字节码是由计算机生成的)。而是因为循环是一个浮点数循环,浮点数运算因其昂贵的特性而出名。因此,内部代码相对于整体运行时间来说是相对较快的,或者可以忽略不计(正如 @j11john 所暗示的那样)。

如果你将循环更改为基于 int,这两种形式的运行速度将会显著不同。

		long start = System.nanoTime();
		int count = (int)Math.pow(10, 9);
		int x = 0;
		for(int i = 0;i<count;i++) {
			int y = 10;
		     x = 3*y;
		    x = 100*y*x;
		    int[] arr = new int[3];
		    arr[0] = 1;
	    
		}
		System.out.println((System.nanoTime()-start)/1_000_000_000.);
	}

8.4248E-5 vs 0.047076639 在加入额外代码的情况下,后者的时间更长。

因此,运行时间的相似性(正如楼主所观察到的)与循环的性质(int vs double)有关,与循环内部的代码关系很小。

英文:

The answer is not because of optimization (or solely thereof - the byte code is generated). It is because the loop is a floating point loop and floating point operations are notoriously expensive. So the internal code is relatively faster or insignificant to the overall running time (as was alluded to by @j11john).

If you change the loop so it is int based, the two forms will run at significantly different speeds.

		long start = System.nanoTime();
		int count = (int)Math.pow(10, 9);
		int x = 0;
		for(int i = 0;i&lt;count;i++) {
			int y = 10;
		     x = 3*y;
		    x = 100*y*x;
		    int[] arr = new int[3];
		    arr[0] = 1;
	    
		}
		System.out.println((System.nanoTime()-start)/1_000_000_000.);
	}

8.4248E-5 vs 0.047076639 The latter time with the extra code.

So the similarity in both running times (as observed by the OP) is related to the nature of the loop (int vs double) and has very little to do with the code within the loop.

答案2

得分: 0

当然,循环内部的代码不会消耗太多的 CPU 资源,因此执行时间可能会保持不变。

英文:

Surely the inside code of the loop isn't cpu expensive so is probably going to have the same time.

答案3

得分: 0

如果循环内的代码不影响之后的代码,编译器将跳过循环内的所有操作以优化性能。

更多详情请查看以下链接:https://en.wikipedia.org/wiki/Java_performance

英文:

If code inside the loop doesn't affect code after it compiler will skip all operations inside the loop to optimize performance.

Check the following link for more details: https://en.wikipedia.org/wiki/Java_performance

huangapple
  • 本文由 发表于 2020年9月23日 05:37:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64018008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定