1 /* 2 * Copyright (C) 2010 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 android.text.format.DateFormat; 20 import com.google.caliper.Runner; 21 import com.google.caliper.SimpleBenchmark; 22 import java.text.SimpleDateFormat; 23 import java.util.Calendar; 24 import java.util.Date; 25 import java.util.GregorianCalendar; 26 27 public final class DateToStringBenchmark extends SimpleBenchmark { 28 Date date; 29 Calendar calendar; 30 SimpleDateFormat format; 31 32 @Override 33 protected void setUp() throws Exception { 34 date = new Date(0); 35 calendar = new GregorianCalendar(); 36 calendar.setTime(date); 37 format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); 38 } 39 40 public void timeDateToString(int reps) throws Exception { 41 for (int i = 0; i < reps; ++i) { 42 date.toString(); 43 } 44 } 45 46 public void timeDateToString_Formatter(int reps) throws Exception { 47 for (int i = 0; i < reps; ++i) { 48 new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(date); 49 } 50 } 51 52 public void timeDateToString_ClonedFormatter(int reps) throws Exception { 53 for (int i = 0; i < reps; ++i) { 54 ((SimpleDateFormat) format.clone()).format(date); 55 } 56 } 57 58 public void timeDateToString_AndroidDateFormat(int reps) { 59 for (int i = 0; i < reps; i++) { 60 DateFormat.format("EEE MMM dd HH:mm:ss zzz yyyy", calendar); 61 } 62 } 63 64 public static void main(String[] args) throws Exception { 65 Runner.main(DateToStringBenchmark.class, args); 66 } 67 } 68