Saturday, March 9, 2013

Calculate time to complete a method or a block of code in Java.

There is no direct way to calculate the time of a mehtod which is in execution. But there is a method to calculate current time by which we can approx calculate the total  time.

Class : System
Method : currentTimeMillis

Use:


  1. public static void main(String[] args) {
  2. long startTime = System.currentTimeMillis();
  3. System.out.println(method(some_value));
  4. long endTime = System.currentTimeMillis();
  5. long totalTime = endTime - startTime;
  6. System.out.println("total time in milli seconds "(totalTime);
  7. }



0 comments: