回复:Adding appendHex method to StringBuilder
温绍锦(高铁)
shaojin.wensj at alibaba-inc.com
Sat Sep 30 19:28:15 UTC 2023
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 <ecki at zusammenkunft.net>
发送时间:2023年10月1日(星期日) 00:50
收件人:温绍锦(高铁) <shaojin.wensj at alibaba-inc.com>; core-libs-dev <core-libs-dev at openjdk.org>
主 题: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 <core-libs-dev-retn at openjdk.org> im Auftrag von 温绍锦(高铁) <shaojin.wensj at alibaba-inc.com>
Gesendet: Samstag, September 30, 2023 2:07 PM
An: core-libs-dev <core-libs-dev at openjdk.org>
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20231001/3bb2c486/attachment-0001.htm>
More information about the core-libs-dev
mailing list