Using the new method, the performance is improved by approximately 26% when 
running on MaxBookPro M1 Max.
```
Benchmark Mode Cnt Score Error Units
StringBuilders.appendHex8 avgt 15 15.259 ? 0.121 ns/op (+26.19%)
StringBuilders.appendHexCombine8 avgt 15 19.256 ? 0.059 ns/op
```
The test code is as follows:
```java
public class StringBuilders {
 @Benchmark
 public String appendHexCombine8() {
 StringBuilder result = new StringBuilder();
 result.append(Integer.toHexString(2048));
 result.append(Integer.toHexString(31337));
 result.append(Integer.toHexString(0xbeefcace));
 result.append(Integer.toHexString(9000));
 result.append(Integer.toHexString(4711));
 result.append(Integer.toHexString(1337));
 result.append(Integer.toHexString(2100));
 result.append(Integer.toHexString(2600));
 return result.toString();
 }
 @Benchmark
 public String appendHex8() {
 StringBuilder result = new StringBuilder();
 result.appendHex(2048);
 result.appendHex(31337);
 result.appendHex(0xbeefcace);
 result.appendHex(9000);
 result.appendHex(4711);
 result.appendHex(1337);
 result.appendHex(2100);
 result.appendHex(2600);
 return result.toString();
 }
}
```
------------------------------------------------------------------
发件人:Bernd <[email protected]>
发送时间:2023年10月1日(星期日) 00:50
收件人:温绍锦(高铁) <[email protected]>; core-libs-dev 
<[email protected]>
主 题:Re: Adding appendHex method to StringBuilder
Or maybe make it more generic and allow to specify the base as well, that’s 
more along the line of existing toString() methods of numbers.
besides convinience, I wonder if it would actually improve performance, how 
good is the JIT and EA in that case, did you run some benchmarks?
Gruss
Bernd
-- 
http://bernd.eckenfels.net <http://bernd.eckenfels.net >
Von: core-libs-dev <[email protected]> im Auftrag von 温绍锦(高铁) 
<[email protected]>
Gesendet: Samstag, September 30, 2023 2:07 PM
An: core-libs-dev <[email protected]>
Betreff: Adding appendHex method to StringBuilder
There are many combinations of `append(Integer.toHexString(i))` and 
`append(Long.toHexString(i))` in the JDK code. I request to add method 
`appendHex(int)` and `appendHex(int)` to StringBuilder and StringBuffer. This 
can reduce duplicate code within the JDK and will also be useful to users.
I submitted a PR ( https://github.com/openjdk/jdk/pull/15998 
<https://github.com/openjdk/jdk/pull/15998 > ), including the code and tests 
for the new method. I also replaced the code that uses the append + toHexString 
combination inside the JDK with appendHex.
Please review and don't hesitate to critique my approach and patch.

Reply via email to