System currentTimeMillis vs System nanoTime

0 votes

When I am updating my object's position in my game, should I use System.currentTimeMillis() or
System.nanoTime()?

In my application, the change in the movement is directly proportional to the elapsed time since the last call. So, I want it to be as precise as possible. Any suggestions or comments on this will be most appreciated.

Aug 10, 2018 in Java by anto.trigg4
• 3,440 points
2,844 views

1 answer to this question.

0 votes
  • In case you want extremely precise measurements of elapsed time then I think you can use System.nanoTime(). It will give you a nanosecond-precise time, relative to some arbitrary point.

  • You can use System.currentTimeMillis() if you want the most accurate possible elapsed time in milliseconds since the epoch.

Below I have quoted a few lines from the Java Documentation:

public static long nanoTime()

Returns the current value of the most precise available system timer, in nanoseconds.

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

For example, to measure how long some code takes to execute:

long startTime = System.nanoTime();    
// ... the code being measured ...    
long estimatedTime = System.nanoTime() - startTime;
answered Aug 10, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

+1 vote
1 answer

concat() vs “+” operator : In Java for String concatenation

Basically, there are two important differences between ...READ MORE

answered Apr 27, 2018 in Java by Rishabh
• 3,620 points
4,442 views
0 votes
1 answer

HashMap vs LinkedHashMap vs TreeMap

Hi, there is no as such difference ...READ MORE

answered May 5, 2018 in Java by v.liyyah
• 1,300 points
1,159 views
0 votes
2 answers

How to get input from user using System.console()?

Scanner in = new Scanner(System.in); int i = ...READ MORE

answered Aug 2, 2018 in Java by Sushmita
• 6,910 points
777 views
0 votes
1 answer

@Component vs @Repository vs @Service in Spring Framework

As you might be knowing, all these ...READ MORE

answered May 29, 2018 in Java by geek.erkami
• 2,680 points
3,293 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,307 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
7,920 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
16,941 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,356 views
0 votes
1 answer

Comparable vs Comparator in Java

I think you are not alone who is ...READ MORE

answered May 11, 2018 in Java by code.reaper12
• 3,500 points
859 views
0 votes
1 answer

Java Timer vs Executor Service

According to Java Concurrency in Practice: Timer is ...READ MORE

answered Jul 26, 2018 in Java by code.reaper12
• 3,500 points
5,638 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP