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。