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

前端 未结 30 2693
北荒
北荒 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 11:46

    There are a couple of ways to do that. I normally fall back to just using something like this:

    long start = System.currentTimeMillis();
    // ... do something ...
    long end = System.currentTimeMillis();
    

    or the same thing with System.nanoTime();

    For something more on the benchmarking side of things there seems also to be this one: http://jetm.void.fm/ Never tried it though.

提交回复
热议问题