简体   繁体   English

.NET JIT 会优化空循环吗?

[英]Does .NET JIT optimize empty loops away?

This article suggests otherwise.这篇文章另有建议。 But there is still a need to evaluate the loop condition.但是仍然需要评估循环条件。 Does java just employ a specific trick to recognize this case? java 是否只是采用了特定的技巧来识别这种情况?

Check out the follow-up story to the article you quote.查看您引用的文章的后续故事

NOTE to people answering: It appears the OP is asking about the .NET JIT, not the Java JIT, since the article referenced suggested that Java did a better job of (or that only Java did) optimizing away empty loops.回答人的注意事项:看来 OP 是在询问 .NET JIT,而不是 Java JIT,因为所引用的文章表明 Java 在优化空循环方面做得更好(或只有 Java 做得更好)。

EDIT: Googling for more answers, Jon Skeet's name keeps coming up.编辑:谷歌搜索更多答案,乔恩斯基特的名字不断出现。 See, for example, this thread on C# optimizations .例如,请参阅有关 C# 优化的线程 Thus, when he answers, we'll have the authoritative answer!这样,当他回答时,我们就有了权威的回答! :-) :-)

Yes it will.是的,它会。

http://www.javaspecialists.eu/archive/Issue158.html http://www.javaspecialists.eu/archive/Issue158.html

At least in Java 5 and 6. The article you linked to is about an older VM.至少在 Java 5 和 6 中。您链接到的文章是关于较旧的 VM。

The article is from 2003. CLI (and java VMs) have advanced a lot since then.这篇文章来自 2003 年。从那时起,CLI(和 Java 虚拟机)有了很大的进步。 In general you have to be very cautious whenever doing micro benchmarks.一般来说,无论何时进行微基准测试,您都必须非常谨慎。 It is real easy to end up measuring jit performance, compiler efficiency at removing dead code, timing overhead, garbage collection, and so on.最终测量 jit 性能、编译器删除死代码的效率、计时开销、垃圾收集等非常容易。

Java doesn't always optimise way empty loops. Java 并不总是优化方式空循环。 In this case it took 2.966 seconds to count 4 BN numbers.在这种情况下,计算 4 BN 数字需要 2.966 秒。

long start = System.nanoTime();
for (int i = Integer.MIN_VALUE; i < Integer.MAX_VALUE; i++);
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to empty loop.%n", time * 1e-9);

Prints印刷

Took 2.966 seconds to empty loop.

This was using Java 6u11, perhaps 6u14 will be smarter.这是使用 Java 6u11,也许 6u14 会更智能。

一般来说,尽量写出尽可能简单的代码,这样 JVM 就可以很好地猜测你要做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM