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

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

问题描述

我想知道哪段代码会运行得更快,因为我想尽可能地优化。

I was wondering which piece of code is going to run faster since I want to optimize as much as possible.

代码A:

if(((a & 0x0FFF) + (b & 0x0FFF)) & 0x1000 != 0)
{
    Register.setHCarryFlag(true);
}
else
{
    Register.setHCarryFlag(false);
}

代码B:

Register.setHCarryFlag(((a & 0x0FFF) + (b & 0x0FFF))& 0x1000 != 0);

我问的原因是我怀疑代码B没有分支,但我不知道确保将它们分别转换为机器代码。

The reason I ask is I suspect that code B doesn't branch, but I don't know for sure how each is converted to machine code.

更好的是,有没有办法查看每段代码生成的机器代码?

Better yet, is there a way to see the machine code that is produced from each piece of code?

推荐答案

无论如何,使用第二种方法都将更加安全,因为从根本上讲,这里没有分支,只有纯粹的计算。一个好的JIT编译器可能会认识到可以消除您在第一个示例中的分支,但是为什么对JIT和人类读者来说却变得更加困难。

However you turn it, you'll be safer with the second approach because there is fundamentally no branching there, just pure calculation. A good JIT compiler may recognize that your branch in the first example can be eliminated, but why make it more difficult for JIT—and for a human reader.

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

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