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

前端 未结 30 2739
北荒
北荒 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 12:00

    Using AOP/AspectJ and @Loggable annotation from jcabi-aspects you can do it easy and compact:

    @Loggable(Loggable.DEBUG)
    public String getSomeResult() {
      // return some value
    }
    

    Every call to this method will be sent to SLF4J logging facility with DEBUG logging level. And every log message will include execution time.

提交回复
热议问题