Integer类中的Java getChars方法,为什么使用位运算而不是算术运算? [英] Java getChars method in Integer class, why is it using bitwise operations instead of arithmetic?

查看:60
本文介绍了Integer类中的Java getChars方法,为什么使用位运算而不是算术运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在检查 Integer 的类

So I was examining the Integer's class source code (JDK 8) to understand how an int get converted to a String. It seems to be using a package private method called getChars (line 433) to convert an int to char array.

尽管代码并不难理解,但是在多行代码中,它们使用按位移位运算而不是简单的算术乘法/除法,例如以下代码行:

While the code is not that difficult to understand, however, there are multiple lines of code where they use bitwise shift operations instead of simple arithmetic multiplication/division, such as the following lines of code:

// really: r = i - (q * 100);
r = i - ((q << 6) + (q << 5) + (q << 2));

q = (i * 52429) >>> (16+3);
r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...

我只是不明白这样做的目的,这实际上是一种优化,并且会影响算法的运行时间吗?

I just do not understand the point of doing that, is this actually an optimization and does it affect the runtime of the algorithm?

换一种说法,由于编译器在内部进行这种类型的优化,因此这种手动优化是否必要?

To put it in a another way, since the compiler does this type of optimization internally, is this manual optimization necessary?

推荐答案

我不知道进行此特定更改的原因,除非找到原始作者,否则不太可能找到权威的答案.

I don't know the reason for this specific change and unless you find the original author, it's unlikely you'll find an authoritative answer to that anyway.

但是我想回应更广泛的观点,那就是运行时库( java.* 和许多内部包)中的很多代码是优化到某种程度以至于无法应用于正常"应用程序代码.(我敢说这是不负责任的.)

But I'd like to respond to the wider point, which is that a lot of code in the runtime library (java.* and many internal packages) is optimized to a degree that would be very unusual (and I dare say irresponsible) to apply to "normal" application code.

这基本上有两个原因:

  1. 在许多不同的环境中它被称为很多.如果每天只在3台服务器上执行50次,那么优化服务器中的方法以减少0.1%的CPU时间将是不值得的.但是,如果您可以使将要执行它的每个人将 Integer.toString 的速度提高0.1%,那么这的确会带来很大的变化.
  2. 如果您在特定VM上优化应用程序代码,则当编译器决定进行其他优化时,将该VM更新到较新版本可以轻松撤消您的优化.使用 java.* 中的代码,这几乎不是问题,因为它总是随运行它的运行时一起提供.因此,如果他们引入了使给定优化不再最佳的编译器更改,那么他们可以更改代码以与此相匹配.
  1. It's called a lot and in many different environment. Optimizing a method in your server to take 0.1% less CPU time when it's only executed 50 times per day on 3 servers each won't be worth the effort you put into it. If, however, you can make Integer.toString 0.1% faster for everyone who will ever execute it, then this can turn into a very big change indeed.
  2. If you optimize your application code on a specific VM then updating that VM to a newer version can easily undo your optimization, when the compiler decides to optimize differently. With code in java.* this is far less of an issue, because it is always shipped with the runtime that will run it. So if they introduce a compiler change that makes a given optimization no longer optimal, then they can change the code to match this.

tl; dr java.* 代码经常被疯狂优化,因为这是值得的,他们可以知道工作.

tl;dr java.* code is often optimized to an insane degree because it's worth it and they can know that it will actually work.

这篇关于Integer类中的Java getChars方法,为什么使用位运算而不是算术运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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