Home | History | Annotate | Download | only in regression
      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.BeforeExperiment;
     21 import java.text.SimpleDateFormat;
     22 import java.util.Calendar;
     23 import java.util.Date;
     24 import java.util.GregorianCalendar;
     25 
     26 public final class DateToStringBenchmark {
     27     Date date;
     28     Calendar calendar;
     29     SimpleDateFormat format;
     30 
     31     @BeforeExperiment
     32     protected void setUp() throws Exception {
     33         date = new Date(0);
     34         calendar = new GregorianCalendar();
     35         calendar.setTime(date);
     36         format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
     37     }
     38 
     39     public void timeDateToString(int reps) throws Exception {
     40         for (int i = 0; i < reps; ++i) {
     41             date.toString();
     42         }
     43     }
     44 
     45     public void timeDateToString_Formatter(int reps) throws Exception {
     46         for (int i = 0; i < reps; ++i) {
     47             new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(date);
     48         }
     49     }
     50 
     51     public void timeDateToString_ClonedFormatter(int reps) throws Exception {
     52         for (int i = 0; i < reps; ++i) {
     53             ((SimpleDateFormat) format.clone()).format(date);
     54         }
     55     }
     56 
     57     public void timeDateToString_AndroidDateFormat(int reps) {
     58         for (int i = 0; i < reps; i++) {
     59             DateFormat.format("EEE MMM dd HH:mm:ss zzz yyyy", calendar);
     60         }
     61     }
     62 }
     63