17fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin/* 27fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * Copyright (C) 2009 Google Inc. 37fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * 47fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * Licensed under the Apache License, Version 2.0 (the "License"); 57fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * you may not use this file except in compliance with the License. 67fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * You may obtain a copy of the License at 77fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * 87fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * http://www.apache.org/licenses/LICENSE-2.0 97fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * 107fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * Unless required by applicable law or agreed to in writing, software 117fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * distributed under the License is distributed on an "AS IS" BASIS, 127fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * See the License for the specific language governing permissions and 147fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * limitations under the License. 157fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin */ 167fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 177fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffinpackage examples; 187fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 19e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffinimport com.google.caliper.Benchmark; 207fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffinimport com.google.caliper.Param; 217fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 227fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin/** 237fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * Measures the various ways the JDK converts doubles to strings. 247fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin */ 25e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffinpublic class DoubleToStringBenchmark { 267fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Param Method method; 277fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 287fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin public enum Method { 297fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin TO_STRING { 307fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Override String convert(double d) { 317fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return ((Double) d).toString(); 327fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 337fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Override String convert(Double d) { 347fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return d.toString(); 357fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 367fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin }, 377fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin STRING_VALUE_OF { 387fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Override String convert(double d) { 397fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return String.valueOf(d); 407fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 417fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Override String convert(Double d) { 427fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return String.valueOf(d); 437fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 447fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin }, 457fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin STRING_FORMAT { 467fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Override String convert(double d) { 477fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return String.format("%f", d); 487fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 497fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Override String convert(Double d) { 507fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return String.format("%f", d); 517fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 527fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin }, 537fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin QUOTE_TRICK { 547fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Override String convert(double d) { 557fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return "" + d; 567fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 577fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Override String convert(Double d) { 587fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return "" + d; 597fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 607fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin }, 617fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin ; 627fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 637fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin abstract String convert(double d); 647fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin abstract String convert(Double d); 657fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 667fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 677fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin enum Value { 687fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin Pi(Math.PI), 697fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin NegativeZero(-0.0), 707fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin NegativeInfinity(Double.NEGATIVE_INFINITY), 717fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin NaN(Double.NaN); 727fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 737fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin final double value; 747fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 757fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin Value(double value) { 767fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin this.value = value; 777fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 787fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 797fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 807fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin @Param Value value; 817fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 82e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffin @Benchmark int primitive(int reps) { 837fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin double d = value.value; 847fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin int dummy = 0; 857fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin for (int i = 0; i < reps; i++) { 867fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin dummy += method.convert(d).length(); 877fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 887fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return dummy; 897fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 907fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin 91e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffin @Benchmark int wrapper(int reps) { 927fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin Double d = value.value; 937fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin int dummy = 0; 947fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin for (int i = 0; i < reps; i++) { 957fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin dummy += method.convert(d).length(); 967fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 977fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin return dummy; 987fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin } 997fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin} 100