SimpleDateFormat和FastDateFormat的效率测试,FastDateFormatr优于SimpleDateFormat

需要引入commons-lang包,Maven配置如下:

 

 <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

 

 

测试代码如下:

import org.apache.commons.lang3.time.FastDateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Discribe:Test
 * 2015/11/12 23:16
 */
public class DateTest {

    public static  void main(String[] args)  {

        /**
         * testDateFormat
         */
        int cont = 200000;
        String pattern = "yyyy-MM-dd HH:mm:ss";
        /**
         * 测试10次
         */
        for(int i=1;i<=10;i++){
            testDateFormat(cont,i,pattern);
        }

    }
    /**
     *  testDateFormat
     */
    public static void testDateFormat(int cont,int num,String pattern){
        System.out.println("执行第"+num+"次");

        long l1 = System.currentTimeMillis();
        for(int i=0;i<=cont;i++){
            SimpleDateFormat s = new SimpleDateFormat(pattern);
            Date date = new Date(System.currentTimeMillis());
            String string = s.format(date);

        }
        long l2 =  System.currentTimeMillis();
        System.out.println("SimpleDateFormat covert time:"+(l2-l1));

        for(int i=0;i<=cont;i++){
            FastDateFormat s = FastDateFormat.getInstance(pattern, TimeZone.getDefault(), Locale.getDefault());
            Date date = new Date(System.currentTimeMillis());
            String string = s.format(date);
            //System.out.println(string);
        }
        long l3 =  System.currentTimeMillis();
        System.out.println("FastDateFormat covert time:"+(l3-l2));

    }
}

 

多次运行结果如下:

 

执行第1次
SimpleDateFormat covert time:1183
FastDateFormat covert time:441
执行第2次
SimpleDateFormat covert time:566
FastDateFormat covert time:155
执行第3次
SimpleDateFormat covert time:322
FastDateFormat covert time:132
执行第4次
SimpleDateFormat covert time:323
FastDateFormat covert time:140
执行第5次
SimpleDateFormat covert time:331
FastDateFormat covert time:130
执行第6次
SimpleDateFormat covert time:292
FastDateFormat covert time:93
执行第7次
SimpleDateFormat covert time:245
FastDateFormat covert time:96
执行第8次
SimpleDateFormat covert time:249
FastDateFormat covert time:98
执行第9次
SimpleDateFormat covert time:246
FastDateFormat covert time:98
执行第10次
SimpleDateFormat covert time:242
FastDateFormat covert time:103

 从运行结果来看,FastDateFormat效率明 SimpleDateFormat 

 

你可能感兴趣的:(SimpleDateFormat和FastDateFormat的效率测试,FastDateFormatr优于SimpleDateFormat)