diff options
author | Neil Fuller <nfuller@google.com> | 2019-06-18 07:25:00 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-06-18 07:25:00 -0700 |
commit | 7bb9a1e67ff8d91f9f9c2f23578a9268839d1d06 (patch) | |
tree | 38d4ffc3a4a2c5bd48a0eba266125479a370cc5c /core/tests/benchmarks | |
parent | cf48a8e8284b157708f3b290019e0bc0e208ffa6 (diff) | |
parent | e25cc6b0e73e99c31368079570dd304f0cfe12d4 (diff) |
Merge "Add a basic benchmark for android.text.format.Time" am: 9038c7231d
am: e25cc6b0e7
Change-Id: I26854156a2f0451622bd70d9dba93b9bee669298
Diffstat (limited to 'core/tests/benchmarks')
-rw-r--r-- | core/tests/benchmarks/src/android/text/format/AndroidTimeVsOthersBenchmark.java | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/core/tests/benchmarks/src/android/text/format/AndroidTimeVsOthersBenchmark.java b/core/tests/benchmarks/src/android/text/format/AndroidTimeVsOthersBenchmark.java new file mode 100644 index 000000000000..ea244001aae4 --- /dev/null +++ b/core/tests/benchmarks/src/android/text/format/AndroidTimeVsOthersBenchmark.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.text.format; + +import com.google.caliper.Benchmark; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; + +public class AndroidTimeVsOthersBenchmark { + + private static final String[] TIMEZONE_IDS = { + "Europe/London", + "America/Los_Angeles", + "Asia/Shanghai", + }; + + @Benchmark + public void toMillis_androidTime(int reps) { + long answer = 0; + for (int i = 0; i < reps; i++) { + String timezoneId = TIMEZONE_IDS[i % TIMEZONE_IDS.length]; + Time time = new Time(timezoneId); + time.set(1, 2, 3, 4, 5, 2010); + answer = time.toMillis(false); + } + // System.out.println(answer); + } + + @Benchmark + public void toMillis_javaTime(int reps) { + long answer = 0; + for (int i = 0; i < reps; i++) { + String timezoneId = TIMEZONE_IDS[i % TIMEZONE_IDS.length]; + LocalDateTime time = LocalDateTime.of(2010, 5 + 1, 4, 3, 2, 1); + ZoneOffset offset = ZoneId.of(timezoneId).getRules().getOffset(time); + answer = time.toInstant(offset).toEpochMilli(); + } + // System.out.println(answer); + } + + @Benchmark + public void toMillis_javaUtil(int reps) { + long answer = 0; + for (int i = 0; i < reps; i++) { + String timezoneId = TIMEZONE_IDS[i % TIMEZONE_IDS.length]; + java.util.TimeZone timeZone = java.util.TimeZone.getTimeZone(timezoneId); + java.util.Calendar calendar = new java.util.GregorianCalendar(timeZone); + calendar.set(2010, 5, 4, 3, 2, 1); + calendar.set(java.util.Calendar.MILLISECOND, 0); + answer = calendar.getTimeInMillis(); + } + // System.out.println(answer); + } + + @Benchmark + public void toMillis_androidIucUtil(int reps) { + long answer = 0; + for (int i = 0; i < reps; i++) { + String timezoneId = TIMEZONE_IDS[i % TIMEZONE_IDS.length]; + android.icu.util.TimeZone timeZone = + android.icu.util.TimeZone.getTimeZone(timezoneId); + android.icu.util.Calendar calendar = new android.icu.util.GregorianCalendar(timeZone); + calendar.set(2010, 5, 4, 3, 2, 1); + calendar.set(android.icu.util.Calendar.MILLISECOND, 0); + answer = calendar.getTimeInMillis(); + } + // System.out.println(answer); + } +} |