OSDN Git Service

Consistently use Formatter's cached NumberFormat.
authorElliott Hughes <enh@google.com>
Mon, 21 Sep 2009 21:08:38 +0000 (14:08 -0700)
committerElliott Hughes <enh@google.com>
Mon, 21 Sep 2009 21:08:38 +0000 (14:08 -0700)
%f is a lot more expensive than it should be because we're not using the cached
NumberFormat (as we are for %d). Running the microbenchmark I added to the
bug (times in ms, on a Cortex A8):

                                old       new
      new Formatter %f         1732       811
      String.format %d          635       651
      String.format %f         1752       900
      reuse Formatter %f       1521       188
      Double.toString +         149       148
      Double.toString append     33        33
      StringBuilder.append      143       139

Bug: 1476

libcore/luni/src/main/java/java/util/Formatter.java

index d1dd417..3196f0e 100644 (file)
@@ -1896,7 +1896,7 @@ public final class Formatter implements Closeable, Flushable {
             }
             // output result
             FloatUtil floatUtil = new FloatUtil(result, formatToken,
-                    (DecimalFormat) NumberFormat.getInstance(locale), arg);
+                    (DecimalFormat) getNumberFormat(), arg);
             floatUtil.transform(formatToken, result);
 
             formatToken.setPrecision(FormatToken.UNSET);