Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • @David Oliván Ubieto 1stly::I can't put System.nanoTime as the very first line in my every Method (To get the execution time) and get difference (as this may be also a wish list in advance that i only know the signature and nothing else) 2ndly::My problem is remain on the same point that "i want to get the execution time taken by the method excluding the overhead taken by invoke() utility of Java Reflection" Kindly give some other direction of help. Commented Apr 7, 2011 at 6:35
  • @zaree If you invoke the method using reflection and you need to measure the execution time of the method without the invocation overhead, you have little possibilities. If we can assume the invocation overhead is much lower than execution time, use nanoTime() before and after the call. If you reuse the Method object and call the method many time, the invocation overhead could be ignored. If you can not starting putting time measurement lines in every method called (inside or outside), use a profiler as stated. Commented Apr 8, 2011 at 4:37
  • @zaree I think profilers impose a general overhead so you can get relative execution time between methods in order to get the methods that consume more time and absolute execution times with some precision (I'm not an expert about profiles). You can use also Java Execution Time Measurement Library (JETM) but imposes to add lines in every method, in contrast simplifies the measurements collection and printing. View also other questions similar. You question is not easy. Commented Apr 8, 2011 at 4:41
  • @David Oliván Ubieto ok let say if we use the System.currentTimeMillis() and as u say put it before and after fun() call then one problem i have face is that it shows poor accuracy.And there is a change among values if we frequently run the program.But sometime a pattern of values repeat.Can u give the reason y this happen? And normally currentTimeMilles is said to be good precision and poor accuracy. Commented Apr 8, 2011 at 8:13
  • @zaree You should use System.nanoTime() for precision timing. As explained in SO and others, this method gets a relative time using CPU counters and its expensive, but is very precise if you want nano precision or greater. System.currentTimeMillins() is very fast as gets the "clock" time of the Operating System, normally reading a global variable, but this time could be precise only at 10ms level, depending on the OS. The "clock" time has not to be accurate at this level as in .NET same occurs with DateTime. If you want relative times with precission, use nanoTime. Commented Apr 8, 2011 at 8:37