Java中哪一段代码更快? [英] Which of these pieces of code is faster in Java?

查看:99
本文介绍了Java中哪一段代码更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a) for(int i = 100000; i> 0; i--){}

b) for(int i = 1; i< 100001; i ++){}

答案在本网站上(问题3)。我只是想不出 为什么? 来自网站:

The answer is there on this website (question 3). I just can't figure out why? From website:


3. a

3. a


推荐答案

当你达到最低级别时(机器代码)但我会使用汇编,因为它主要是一对一映射,空循环递减到0和一个递增到50(例如)之间的差异通常是这样的:

When you get down to the lowest level (machine code but I'll use assembly since it maps one-to-one mostly), the difference between an empty loop decrementing to 0 and one incrementing to 50 (for example) is often along the lines of:

      ld  a,50                ld  a,0
loop: dec a             loop: inc a
      jnz loop                cmp a,50
                              jnz loop

这是因为当你达到零时,大多数理智CPU中的零标志由递减指令设置。当增量指令达到50时,通常不能说同样的增量指令(因为该值没有什么特别的,不像零)。因此,您需要将寄存器与50进行比较以设置零标记。

That's because the zero flag in most sane CPUs is set by the decrement instruction when you reach zero. The same can't usually be said for the increment instruction when it reaches 50 (since there's nothing special about that value, unlike zero). So you need to compare the register with 50 to set the zero flag.

然而,询问两个循环中的哪一个:

However, asking which of the two loops:

for(int i = 100000; i > 0; i--) {}
for(int i = 1; i < 100001; i++) {}

更快(几乎任何环境,Java或其他)都是无用的,因为它们都没有做任何有用的事情。这两个循环的最快版本根本没有循环。我挑战任何人提出一个比这更快的版本: - )

is faster (in pretty much any environment, Java or otherwise) is useless since neither of them does anything useful. The fastest version of both those loops no loop at all. I challenge anyone to come up with a faster version than that :-)

当你开始在括号内做一些有用的工作时,它们才会变得有用点,工作将决定你应该使用哪个订单。

They'll only become useful when you start doing some useful work inside the braces and, at that point, the work will dictate which order you should use.

例如,如果你需要从1开始计算到100,000,你应该使用第二个循环。这是因为每次你需要使用它时,你必须在循环内评估 100000-i 这一事实可能会淹没倒计时(如果有的话)的优势。在汇编方面,这将是:

For example if you need to count from 1 to 100,000, you should use the second loop. That's because the advantage of counting down (if any) is likely to be swamped by the fact that you have to evaluate 100000-i inside the loop every time you need to use it. In assembly terms, that would be the difference between:

     ld  b,100000             dsw a
     sub b,a
     dsw b

dsw 是当然,臭名昭着的汇编程序助记符做一些事情。)

(dsw is, of course, the infamous do something with assembler mnemonic).

因为你只会受到打击对于每次迭代一次递增循环,并且每次迭代你将为减法至少一次(假设你将使用 i ,否则根本不需要循环),你应该选择更自然的版本。

Since you'll only be taking the hit for an incrementing loop once per iteration, and you'll be taking the hit for the subtraction at least once per iteration (assuming you'll be using i, otherwise there's little need for the loop at all), you should just go with the more natural version.

如果你需要数数,请计算。如果你需要倒计时,倒计时。

If you need to count up, count up. If you need to count down, count down.

这篇关于Java中哪一段代码更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆