Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculating Time taken by a code segment  RSS feed

 
Vamsh Lingampally
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would like to know if there are any methods to get the timeofday() (C eqivalent routine) in Java.
I wanted to calculate time taken for execution of certain part of my code and was not sure how to do it. I thought I can use getimeofday() methods if available in Java before the start of the code and at the end of the code whose performance I am checking.
Can any one please help me with it.....
Or if any one knows how to calculate the time taken for execution of a program in Java.
I am running on a dead line and would appreciate if anyone gives me an idea as how to go about getting the performance of the code.
Regards,
Vamsh
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static method System.currentTimeMillis() gives the system clock time in milliseconds, as a long. This is what people use for timing.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36432
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, note that the Java methods for getting time cannot distinguish between very close times. A difference of less than 10 milliseconds is extremely unreliable and may reported as 0 or 10, but nothing in between. I remember reading that this method should not be called more than once or twice a second for reliable performance results.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The precision is platform-dependent. On Windows it's based, I believe, on a heartbeat that ticks 60 times a second, so the resolution is about 17 ms. On other platforms the results will usually be better.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vamsh,
Have you read Vladimir Roubtsov's My kingdom for a good timer! article (from JavaWorld)?
Hope it helps.
Good Luck,
Avi.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out JAMon at htt://www.jamonapi.com for a timer that measures execution time (average, min, max, standard deviation), hits, and whether or not code is currently executing, and more. It can also measure application scalability, can be used in a production environment and is fast.
I have found that although the timer resolution in windows has a granualarity of around 10 ms., if enough measurements are taken the execution time comes out to be close to the average. For example say we have 5 measurements of 10 ms, 0 ms, 0 ms, 10 ms, and 0 ms. Although the individual numbers of 10 ms. and 0 ms. are off, the average comes close to the actual time.
steve - http://www.jamonapi.com
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For profiling you can also look at the open source tool at http://ejp.sourceforge.net, or use the java hprof command depending on the type of app you are working on this can be useful. I've never used the sourceforge tool.
java -Xrunhprof:cpu=samples,depth=6 com.develop.demos.TestHprof
steve - http://www.jamonapi.com
 
Naveen Kuppili
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My two cents - Conventional profiling techniques normally distort the profiling information very much due to the overhead associated with hprof or JVMPI. If you can afford it - get a classloader based profiling tool. This would give you a more accurate picture of ur pefromance metrics.
Originally posted by steve souza:
For profiling you can also look at the open source tool at http://ejp.sourceforge.net, or use the java hprof command depending on the type of app you are working on this can be useful. I've never used the sourceforge tool.
java -Xrunhprof:cpu=samples,depth=6 com.develop.demos.TestHprof
steve - http://www.jamonapi.com
 
Scott Farquhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Similar to JAMon, but also with a servlet filter, and dynamic class proxying, you may also want to check out Atlassian Profiler:
http://opensource.atlassian.com/profiling/example.html
Opensource.
Cheers,
Scott
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!