• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Time calculation in Java

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Date.getTime() and getcurrenttimemillis methods return time in milliseconds.Can anybody please explain what times these values represent? Is it system time or user time or CPU(system+user) time??

Also the Java documentation for the getcurrenttimemillis indicates that although the method returns time in ms, the actual granularity is dependent on the Operating system. The finest possible unit in which the time can be measured is said to be the granularity.

Are these the best possible ways to calculate the time spent on an application in Java? Or is there a way that we can get the granularity of the OS that we are working on and compensate for any differences that might arise in calculating the time??

Javadoc for the getcurrenttimemillis says the following

Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venkata Pavan Kumar vemuri wrote:Can anybody please explain what times these values represent? Is it system time or user time or CPU(system+user) time??


You already read the API documentation of the methods, it explains exactly what the value represents:

public long getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.


It returns the wall clock time, it has nothing to do with CPU (user or system) time.

You can use System.currentTimeMillis() calls to do a somewhat crude calculation of how long your program takes to run, but it's not useful for measuring very short durations of time (less than a second) very accurately. Since Java 5, there's also a method System.nanoTime() which might be more accurate, but there are no guarantees.

Use profiling software to accurately measure execution time of your program.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the date.getTime() method returns time in milliseconds from 1970. it doesnot have anything to do with cpu. if you need different formats of time see java.text.simpleDateFormat. Read about that
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:You can use System.currentTimeMillis() calls to do a somewhat crude calculation of how long your program takes to run, but it's not useful for measuring very short durations of time (less than a second) very accurately.


I wouldn't put the limit at a second; System.currentTimeMillis() usually uses a step size of 10ms. I do agree with advising System.nanoTime(). TimeUnit can then be used to convert this to any other unit you need.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic