What I would like to know is whether I should use System.currentTimeMillis() or System.nanoTime()<
For game graphics & smooth position updates, use System.nanoTime()
rather than System.currentTimeMillis()
. I switched from currentTimeMillis() to nanoTime() in a game and got a major visual improvement in smoothness of motion.
While one millisecond may seem as though it should already be precise, visually it is not. The factors nanoTime()
can improve include:
As other answers suggest, nanoTime does have a performance cost if called repeatedly -- it would be best to call it just once per frame, and use the same value to calculate the entire frame.