RFR: 8336741: Optimize LocalTime.toString with StringBuilder.repeat [v3]

Shaojin Wen duke at openjdk.org
Thu Jul 18 15:33:31 UTC 2024


On Thu, 18 Jul 2024 15:28:23 GMT, Shaojin Wen <duke at openjdk.org> wrote:

>> class LocalTime {
>>     public String toString() {
>>         // ...
>>                 if (nanoValue % 1000_000 == 0) {
>>                     buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
>>                 } else if (nanoValue % 1000 == 0) {
>>                     buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
>>                 } else {
>>                     buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
>>                 }
>>        // ...
>>     }
>> }
>> 
>> Currently, LocalTime.toString handles nanos by adding a value and then subString(1) to fill it with zeros. Using StringBuilder.repeat is more concise and has better performance.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   breaking on every 3 digits

Here are the performance numbers running on a MacBook M1 Max, 182.06% performance improvement


-Benchmark                         Mode  Cnt  Score   Error   Units (master)
-ToStringBench.localTimeToString  thrpt   15  7.752 ? 1.036  ops/ms

+Benchmark                         Mode  Cnt   Score   Error   Units (current ac30276105e541dac48e0d11f4595f93202031b8)
+ToStringBench.localTimeToString  thrpt   15  21.866 ? 0.417  ops/ms +182.06%

-------------

PR Comment: https://git.openjdk.org/jdk/pull/20232#issuecomment-2236864823


More information about the core-libs-dev mailing list