Date、System、Calendar獲取當前時間毫秒

Date、System、Calendar三個類均可以獲取getTime方法,若要考慮性能,應選哪個才比較合適呢?java

以下代碼模擬所示:ide

public class Test35
{函數

    public static void main(String[] args)
    {
        long time = 0L;
        // 執行函數SCTM
        time = SCTM();
        // 獲取時間差並打印
        System.out.println("System.currentTimeMillis cost [" + time + "] ms");
        // 執行函數DGT
        time = DGT();
        // 獲取時間差並打印
        System.out.println("new Date().getTime cost [" + time + "] ms");
        // 執行函數G
        time = GGIGTG();
        // 獲取時間差並打印
        System.out.println("Calendar.getInstance().getTime().getTime cost [" + time + "] ms");性能

    }this

    /**
     * new Date對象放入堆中即消耗內存,在經過System類引用靜態方法獲取 構造方法:this(System.currentTimeMillis())
     * 
     * @return
     */

    private static long DGT()
    {
        // 開啓空間1000000
        List<Long> list = new ArrayList<>(10000000);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++ )
        {
            long data = new Date().getTime();
            list.add(data);
        }
        return (System.currentTimeMillis() - startTime);
    }.net

    /**
     * 直接經過類引用靜態方法獲取
     * 
     * @return
     */

    private static long SCTM()
    {
        // 開啓空間1000000
        List<Long> list = new ArrayList<>(10000000);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++ )
        {
            long data = System.currentTimeMillis();
            list.add(data);
        }
        return (System.currentTimeMillis() - startTime);
    }code

    /**
     * Calendar獲取getInstance靜態方法,但它調用createCalendar方法耗盡了大量時間去判斷邏輯代碼,如createCalendar源碼所示
     * createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
     * 
     * @return
     */

    private static long GGIGTG()
    {
        // 開啓空間1000000
        List<Long> list = new ArrayList<>(10000000);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++ )
        {
            long data = Calendar.getInstance().getTime().getTime();
            list.add(data);
        }
        return (System.currentTimeMillis() - startTime);
    }orm

    /**
     * createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));源碼
     * 
     * @see JDKcode createCalendar(zone,aLocale))
     */

    private static Calendar createCalendar(TimeZone zone, Locale aLocale)
    {
        CalendarProvider provider = LocaleProviderAdapter.getAdapter(CalendarProvider.class,
            aLocale).getCalendarProvider();
        if (provider != null)
        {
            try
            {
                return provider.getInstance(zone, aLocale);
            }
            catch (IllegalArgumentException iae)
            {
                // fall back to the default instantiation
            }
        }對象

        Calendar cal = null;內存

        if (aLocale.hasExtensions())
        {
            String caltype = aLocale.getUnicodeLocaleType("ca");
            if (caltype != null)
            {
                switch (caltype)
                {
                    case "buddhist":
                        cal = new BuddhistCalendar(zone, aLocale);
                        break;
                    case "japanese":
                        cal = new JapaneseImperialCalendar(zone, aLocale);
                        break;
                    case "gregory":
                        cal = new GregorianCalendar(zone, aLocale);
                        break;
                }
            }
        }
        if (cal == null)
        {
            // If no known calendar type is explicitly specified,
            // perform the traditional way to create a Calendar:
            // create a BuddhistCalendar for th_TH locale,
            // a JapaneseImperialCalendar for ja_JP_JP locale, or
            // a GregorianCalendar for any other locales.
            // NOTE: The language, country and variant strings are interned.
            if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH")
            {
                cal = new BuddhistCalendar(zone, aLocale);
            }
            else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
                     && aLocale.getCountry() == "JP")
            {
                cal = new JapaneseImperialCalendar(zone, aLocale);
            }
            else
            {
                cal = new GregorianCalendar(zone, aLocale);
            }
        }
        return cal;
    }

}

運行結果:

System.currentTimeMillis cost [2266] ms
new Date().getTime cost [3051] ms
Calendar.getInstance().getTime().getTime cost [6449] ms

注:方法耗時描述如javadoc註釋所示,由結果可知耗時最可能是Calendar,其次是Date,耗時最少是System。

相關文章
相關標籤/搜索