Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

java - 如何在Java中计时方法的执行时间?(How do I time a method's execution in Java?)

How do I get a method's execution time?

(如何获得方法的执行时间?)

Is there a Timer utility class for things like timing how long a task takes, etc?

(是否存在Timer实用程序类,用于对任务花费多长时间进行计时等?)

Most of the searches on Google return results for timers that schedule threads and tasks, which is not what I want.

(Google上的大多数搜索都会返回安排线程和任务的计时器的结果,这不是我想要的。)

  ask by Ogre Psalm33 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is always the old-fashioned way:

(总有一种老式的方式:)

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

long duration = (endTime - startTime);  //divide by 1000000 to get milliseconds.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...