How do I time a method's execution in Java?

前端 未结 30 2704
北荒
北荒 2020-11-21 11:15
  1. How do I get a method\'s execution time?
  2. Is there a Timer utility class for things like timing how long a task takes, etc?

Mos

30条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 12:02

    I modified the code from correct answer to get result in seconds:

    long startTime = System.nanoTime();
    
    methodCode ...
    
    long endTime = System.nanoTime();
    double duration = (double)(endTime - startTime) / (Math.pow(10, 9));
    Log.v(TAG, "MethodName time (s) = " + duration);
    

提交回复
热议问题