Alan Viverette | 3da604b | 2020-06-10 18:34:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Google Inc. |
| 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 | package benchmarks.regression; |
| 18 | |
| 19 | import java.text.Collator; |
| 20 | import java.text.DateFormat; |
| 21 | import java.text.DateFormatSymbols; |
| 22 | import java.text.DecimalFormatSymbols; |
| 23 | import java.text.NumberFormat; |
| 24 | import java.text.SimpleDateFormat; |
| 25 | import java.util.GregorianCalendar; |
| 26 | import java.util.Locale; |
| 27 | |
| 28 | /** |
| 29 | * Benchmarks creation and cloning various expensive objects. |
| 30 | */ |
| 31 | public class ExpensiveObjectsBenchmark { |
| 32 | public void timeNewDateFormatTimeInstance(int reps) { |
| 33 | for (int i = 0; i < reps; ++i) { |
| 34 | DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); |
| 35 | df.format(System.currentTimeMillis()); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public void timeClonedDateFormatTimeInstance(int reps) { |
| 40 | DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); |
| 41 | for (int i = 0; i < reps; ++i) { |
| 42 | ((DateFormat) df.clone()).format(System.currentTimeMillis()); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | public void timeReusedDateFormatTimeInstance(int reps) { |
| 47 | DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); |
| 48 | for (int i = 0; i < reps; ++i) { |
| 49 | synchronized (df) { |
| 50 | df.format(System.currentTimeMillis()); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | public void timeNewCollator(int reps) { |
| 56 | for (int i = 0; i < reps; ++i) { |
| 57 | Collator.getInstance(Locale.US); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public void timeClonedCollator(int reps) { |
| 62 | Collator c = Collator.getInstance(Locale.US); |
| 63 | for (int i = 0; i < reps; ++i) { |
| 64 | c.clone(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public void timeNewDateFormatSymbols(int reps) { |
| 69 | for (int i = 0; i < reps; ++i) { |
| 70 | new DateFormatSymbols(Locale.US); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public void timeClonedDateFormatSymbols(int reps) { |
| 75 | DateFormatSymbols dfs = new DateFormatSymbols(Locale.US); |
| 76 | for (int i = 0; i < reps; ++i) { |
| 77 | dfs.clone(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public void timeNewDecimalFormatSymbols(int reps) { |
| 82 | for (int i = 0; i < reps; ++i) { |
| 83 | new DecimalFormatSymbols(Locale.US); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public void timeClonedDecimalFormatSymbols(int reps) { |
| 88 | DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); |
| 89 | for (int i = 0; i < reps; ++i) { |
| 90 | dfs.clone(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public void timeNewNumberFormat(int reps) { |
| 95 | for (int i = 0; i < reps; ++i) { |
| 96 | NumberFormat.getInstance(Locale.US); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public void timeClonedNumberFormat(int reps) { |
| 101 | NumberFormat nf = NumberFormat.getInstance(Locale.US); |
| 102 | for (int i = 0; i < reps; ++i) { |
| 103 | nf.clone(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public void timeNumberFormatTrivialFormatLong(int reps) { |
| 108 | NumberFormat nf = NumberFormat.getInstance(Locale.US); |
| 109 | for (int i = 0; i < reps; ++i) { |
| 110 | nf.format(1024L); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public void timeLongToString(int reps) { |
| 115 | for (int i = 0; i < reps; ++i) { |
| 116 | Long.toString(1024L); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public void timeNumberFormatTrivialFormatDouble(int reps) { |
| 121 | NumberFormat nf = NumberFormat.getInstance(Locale.US); |
| 122 | for (int i = 0; i < reps; ++i) { |
| 123 | nf.format(1024.0); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | public void timeNewSimpleDateFormat(int reps) { |
| 128 | for (int i = 0; i < reps; ++i) { |
| 129 | new SimpleDateFormat(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public void timeClonedSimpleDateFormat(int reps) { |
| 134 | SimpleDateFormat sdf = new SimpleDateFormat(); |
| 135 | for (int i = 0; i < reps; ++i) { |
| 136 | sdf.clone(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | public void timeNewGregorianCalendar(int reps) { |
| 141 | for (int i = 0; i < reps; ++i) { |
| 142 | new GregorianCalendar(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | public void timeClonedGregorianCalendar(int reps) { |
| 147 | GregorianCalendar gc = new GregorianCalendar(); |
| 148 | for (int i = 0; i < reps; ++i) { |
| 149 | gc.clone(); |
| 150 | } |
| 151 | } |
| 152 | } |