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

前端 未结 30 2773
北荒
北荒 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条回答
  •  猫巷女王i
    2020-11-21 11:50

    You can use stopwatch class from spring core project:

    Code:

    StopWatch stopWatch = new StopWatch()
    stopWatch.start();  //start stopwatch
    // write your function or line of code.
    stopWatch.stop();  //stop stopwatch
    stopWatch.getTotalTimeMillis() ; ///get total time
    

    Documentation for Stopwatch: Simple stop watch, allowing for timing of a number of tasks, exposing total running time and running time for each named task. Conceals use of System.currentTimeMillis(), improving the readability of application code and reducing the likelihood of calculation errors. Note that this object is not designed to be thread-safe and does not use synchronization. This class is normally used to verify performance during proof-of-concepts and in development, rather than as part of production applications.

提交回复
热议问题