1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 public class StringBuilderAppendBenchmark { 18 public static String string1 = "s1"; 19 public static String string2 = "s2"; 20 public static String longString1 = "This is a long string 1"; 21 public static String longString2 = "This is a long string 2"; 22 public static int int1 = 42; 23 public static double double1 = 42.0; 24 public static double double2 = 1.0E308; 25 public static float float1 = 42.0f; 26 public static float float2 = 1.0E38f; 27 timeAppendStrings(int count)28 public void timeAppendStrings(int count) { 29 String s1 = string1; 30 String s2 = string2; 31 int sum = 0; 32 for (int i = 0; i < count; ++i) { 33 String result = s1 + s2; 34 sum += result.length(); // Make sure the append is not optimized away. 35 } 36 if (sum != count * (s1.length() + s2.length())) { 37 throw new AssertionError(); 38 } 39 } 40 timeAppendLongStrings(int count)41 public void timeAppendLongStrings(int count) { 42 String s1 = longString1; 43 String s2 = longString2; 44 int sum = 0; 45 for (int i = 0; i < count; ++i) { 46 String result = s1 + s2; 47 sum += result.length(); // Make sure the append is not optimized away. 48 } 49 if (sum != count * (s1.length() + s2.length())) { 50 throw new AssertionError(); 51 } 52 } 53 timeAppendStringAndInt(int count)54 public void timeAppendStringAndInt(int count) { 55 String s1 = string1; 56 int i1 = int1; 57 int sum = 0; 58 for (int i = 0; i < count; ++i) { 59 String result = s1 + i1; 60 sum += result.length(); // Make sure the append is not optimized away. 61 } 62 if (sum != count * (s1.length() + Integer.toString(i1).length())) { 63 throw new AssertionError(); 64 } 65 } 66 timeAppendStringAndDouble(int count)67 public void timeAppendStringAndDouble(int count) { 68 String s1 = string1; 69 double d1 = double1; 70 int sum = 0; 71 for (int i = 0; i < count; ++i) { 72 String result = s1 + d1; 73 sum += result.length(); // Make sure the append is not optimized away. 74 } 75 if (sum != count * (s1.length() + Double.toString(d1).length())) { 76 throw new AssertionError(); 77 } 78 } 79 timeAppendStringAndHugeDouble(int count)80 public void timeAppendStringAndHugeDouble(int count) { 81 String s1 = string1; 82 double d2 = double2; 83 int sum = 0; 84 for (int i = 0; i < count; ++i) { 85 String result = s1 + d2; 86 sum += result.length(); // Make sure the append is not optimized away. 87 } 88 if (sum != count * (s1.length() + Double.toString(d2).length())) { 89 throw new AssertionError(); 90 } 91 } 92 timeAppendStringAndFloat(int count)93 public void timeAppendStringAndFloat(int count) { 94 String s1 = string1; 95 float f1 = float1; 96 int sum = 0; 97 for (int i = 0; i < count; ++i) { 98 String result = s1 + f1; 99 sum += result.length(); // Make sure the append is not optimized away. 100 } 101 if (sum != count * (s1.length() + Float.toString(f1).length())) { 102 throw new AssertionError(); 103 } 104 } 105 timeAppendStringAndHugeFloat(int count)106 public void timeAppendStringAndHugeFloat(int count) { 107 String s1 = string1; 108 float f2 = float2; 109 int sum = 0; 110 for (int i = 0; i < count; ++i) { 111 String result = s1 + f2; 112 sum += result.length(); // Make sure the append is not optimized away. 113 } 114 if (sum != count * (s1.length() + Float.toString(f2).length())) { 115 throw new AssertionError(); 116 } 117 } 118 timeAppendStringDoubleStringAndFloat(int count)119 public void timeAppendStringDoubleStringAndFloat(int count) { 120 String s1 = string1; 121 String s2 = string2; 122 double d1 = double1; 123 float f1 = float1; 124 int sum = 0; 125 for (int i = 0; i < count; ++i) { 126 String result = s1 + d1 + s2 + f1; 127 sum += result.length(); // Make sure the append is not optimized away. 128 } 129 if (sum != count * (s1.length() + 130 Double.toString(d1).length() + 131 s2.length() + 132 Float.toString(f1).length())) { 133 throw new AssertionError(); 134 } 135 } 136 } 137