How to find time taken to run a Java program?

后端 未结 7 1933
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 06:17

I have a Java application that I\'ve been working on and I just realized that the program has to return a value in less than a minute, but don\'t know how to find or display

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-24 06:40

    I like using the Apache Commons StopWatch class when I have the library available.

    import org.apache.commons.lang3.time.StopWatch;
    
    // ...
    
    StopWatch stopWatch = new StopWatch();
    String message = "Task : %s (%s) seconds";
    
    // ...
    
    stopWatch.split();
    System.out.println(String.format(message, "10", stopWatch.toSplitString()));
    
    // ...
    
    stopWatch.split();
    System.out.println(String.format(message, "20", stopWatch.toSplitString()));
    
    stopWatch.stop();
    
    

提交回复
热议问题