Java getChars method in Integer class, why is it using bitwise operations instead of arithmetic?

我与影子孤独终老i 提交于 2020-07-03 07:19:11

问题


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));

and

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?

Edit:

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


回答1:


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.

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.

And that has basically two reasons:

  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.* code is often optimized to an insane degree because it's worth it and they can know that it will actually work.




回答2:


There are a couple reasons that this is done. Being a long-time embedded developer, using tiny microcontrollers that sometimes didn't even have a multiplication and division instruction, I can tell you that this is significantly faster. The key here is that the multiplier is a constant. If you were multiplying two variables, you'd either need to use the slower multiply and divide operators or, if they didn't exist, perform multiplication using a loop with the add operator.



来源:https://stackoverflow.com/questions/62064727/java-getchars-method-in-integer-class-why-is-it-using-bitwise-operations-instea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!