Integrated: 8336741: Optimize LocalTime.toString with StringBuilder.repeat

Shaojin Wen duke at openjdk.org
Wed Jul 24 22:42:38 UTC 2024


On Thu, 18 Jul 2024 11:32:47 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.

This pull request has now been integrated.

Changeset: 0898ab7f
Author:    Shaojin Wen <shaojin.wensj at alibaba-inc.com>
Committer: Chen Liang <liach at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/0898ab7f7496689e5de52a5dc4530ca21def1fca
Stats:     164 lines in 3 files changed: 158 ins; 0 del; 6 mod

8336741: Optimize LocalTime.toString with StringBuilder.repeat

Reviewed-by: liach, rriggs, naoto

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

PR: https://git.openjdk.org/jdk/pull/20232


More information about the core-libs-dev mailing list