Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
Nov 30, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 A
Date start = new Date();
//after some thing
Date end  = new Date();
//Do some thing with start and end to find the time difference 

//block B
long start = System.currentTimeMills();
//after some thing
long 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.
Raveendra Babu
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date