Use System.currentTimeMills(); Instead of new Date();

This tip is for when you want to place an action after a time gap between two occurrences (for example, to invalidate an unused session after some permitted time, or to note the total time use of a resource). Instead of storing the starting time of the first occurrence (event) and the ending time of next occurrence (event) as in block A, follow the procedure shown in block B.

 //block ADate start = new Date();//after some thingDate end  = new Date();//Do some thing with start and end to find the time difference //block Blong start = System.currentTimeMills();//after some thinglong end  = System.currentTimeMills();long timeUsed = end - start;//do something with timeUsed


The technique shown in block B avoids the unnecessary creation and garbage collection of two Objects, hence increasing performance.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: