两种,统计各个方法的执行时间的写法

public class T1 {
    @Test
    void test01(){
        long startTime = System.currentTimeMillis();
        log.info("方法1开始执行,开始时间:{}",startTime);
        method01();
        long endTime = System.currentTimeMillis();
        log.info("方法1执行结束,结束时间:{}",endTime);
        log.info("方法1执行耗时:{}毫秒",endTime-startTime);
    }
    @Test
    void test02(){
        StopWatch stopWatch = new StopWatch("test02");
        stopWatch.start("method01");
        method01();
        stopWatch.stop();
        stopWatch.start("method02");
        method02();
        stopWatch.stop();
        log.info("执行耗时情况如下:{}",stopWatch.prettyPrint());

    }

    public void method01(){

        for (int i = 0; i < 10; i++){
            try{
                Thread.sleep(100);
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    }
    public void method02(){
        for (int i = 0; i < 14; i++){
            try{
                Thread.sleep(100);
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    }
}
posted @ 2024-08-21 14:13  冷风5997  阅读(4)  评论(0编辑  收藏  举报