1. 程式人生 > >Date、System、Calendar獲取當前時間毫秒

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

開發十年,就只剩下這套架構體系了! >>>   

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

如下程式碼模擬所示:

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");

    }

    /**
     * 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);
    }

    /**
     * 直接通過類引用靜態方法獲取
     * 
     * @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);
    }

    /**
     * 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);
    }

    /**
     * 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,耗時最少