Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 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; |
Vladimir Marko | 41de450 | 2019-05-21 10:00:15 +0100 | [diff] [blame] | 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; |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 27 | |
| 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 | |
| 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 | |
| 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 | } |
Vladimir Marko | 41de450 | 2019-05-21 10:00:15 +0100 | [diff] [blame] | 66 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | } |
Vladimir Marko | 552a134 | 2017-10-31 10:56:47 +0000 | [diff] [blame] | 136 | } |