private static int
LOOP_NUM
=
1000000
public static void main(String
[]
args) {
long
t1
= System.currentTimeMillis()
testSystem(LOOP_NUM)
long
t2
= System.currentTimeMillis()
System.out.println("testSystem:" + (t2 - t1))
t1
= System.currentTimeMillis()
testCalender(LOOP_NUM)
t2
= System.currentTimeMillis()
System.out.println("testCalender:" + (t2 - t1))
t1
= System.currentTimeMillis()
testDate(LOOP_NUM)
t2
= System.currentTimeMillis()
System.out.println("testDate:" + (t2 - t1))
// test System.currentTimeMillis()
private static void testSystem(int loopNum) {
for (int
i
=
0
long
currentSystemTime
= System.currentTimeMillis()
// test Calendar.getInstance().getTimeInMillis()
private static void testCalender(int loopNum) {
for (int
i
=
0
long
currentTime
= Calendar.getInstance().getTimeInMillis()
private static void testDate(int loopNum) {
for (int
i
=
0
long
currentTime
= new Date().getTime()
输出结果:
testSystem:42
testCalender:417
testDate:36
new Date().getTime()的性能最高,有些设备可能System.currentTimeMillis()最高
。
System.currentTimeMillis()性能仅次于new Date().getTime()。
Calendar.getInstance().getTimeInMillis()性能最低。
如果关注性能相关的问题,使用时间戳时,要尽量减少Calendar.getInstance().getTimeInMillis()的使用。