其他分享
首页 > 其他分享> > 统计代码耗时

统计代码耗时

作者:互联网

计算时间差

import java.util.concurrent.TimeUnit;

public class TimeDiffTest {

    public static void main(String[] args) throws InterruptedException {
        long startMs = System.currentTimeMillis();

        TimeUnit.SECONDS.sleep(2);

        System.out.println("timeCost: " + (System.currentTimeMillis() - startMs));
    }

}

  

org.springframework.util.StopWatch  

import com.alibaba.fastjson.JSON;
import org.springframework.util.StopWatch;

import java.util.concurrent.TimeUnit;

public class StopWatchTest {

    public static void main(String[] args) throws InterruptedException {
        StopWatch stopWatch = new StopWatch();


        stopWatch.start("function1");
        TimeUnit.SECONDS.sleep(1); // 模拟业务代码
        stopWatch.stop();

        stopWatch.start("function2");
        TimeUnit.SECONDS.sleep(1); // 模拟业务代码
        stopWatch.stop();

        System.out.println(JSON.toJSONString(stopWatch.getTaskInfo()));
        System.out.println(stopWatch.prettyPrint());

    }
}

  

标签:StopWatch,代码,System,耗时,import,stopWatch,TimeUnit,public,统计
来源: https://www.cnblogs.com/ooo0/p/16189865.html