This is the 15th day of my participation in the Gwen Challenge.More article challenges

Set a Flag write something every day and stick to it.

A tool for counting code time

In a project, the printing time is usually used to check the time consuming of a certain task, and t2-T1 statistics are usually used for project optimization

public static void main(String[] args) throws InterruptedException {
     StopWatchTest.test0();
// StopWatchTest.test1();
}

public static void test0(a) throws InterruptedException {
     long start = System.currentTimeMillis();
     // do something
     Thread.sleep(100);
    long end = System.currentTimeMillis();
    long start2 = System.currentTimeMillis();
    // do something
    Thread.sleep(200);
    long end2 = System.currentTimeMillis();
    System.out.println("Task 1 Execution Time :" + (end - start));
    System.out.println("Task 2 Execution Time :" + (end2 - start2));
}

Copy the code

Spring StopWatch usage

Spring-framework provides a StopWatch class to control the execution time of similar tasks, that is, encapsulate a Java class to record the start time, end time operations, a small example as follows

Chestnut:

package com.example.stopwatch;

import org.springframework.util.StopWatch;


public class TestStopWatch {
    private void test(a) throws InterruptedException {
        StopWatch sw = new StopWatch();

        sw.start("Task 1");
        Thread.sleep(1000);
        sw.stop();

        sw.start(Task 2 "");
        Thread.sleep(2000);
        sw.stop();

        sw.start("Task 3");
        Thread.sleep(500);
        sw.stop();

        System.out.println(sw.prettyPrint());
        System.out.println(sw.getTotalTimeMillis());
        System.out.println(sw.getLastTaskName());
        System.out.println(sw.getLastTaskInfo());
        System.out.println(sw.getTaskCount());
    }


    public static void main(String []argv) throws InterruptedException {
        TestStopWatch testStopWatch = newTestStopWatch(); testStopWatch.test(); }}Copy the code

The results of

StopWatch ' ': running time (millis) = 3518 ----------------------------------------- ms % Task name -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - task 1 00998 028%, 02020 057% task 2 00500 task 3 3518 mission 3 014% org.springframework.util.StopWatch$TaskInfo@5b2133b1
3
Copy the code

A StopWatch instance can start only one task at a time, but cannot start multiple tasks at a time. A new task cannot be started until the task is stopped. To start multiple tasks at a time, Need new different StopWatch instances.

Timer tool -TimeInterval

Hutool implements the timer function by encapsulating TimeInterval, that is, the time at which a method or procedure is executed.

TimeInterval Supports group timing to facilitate time comparison.

use

TimeInterval timer = DateUtil.timer();

//---------------------------------
//------- This is the execution process
//---------------------------------

timer.interval();// takes milliseconds
timer.intervalRestart();// Returns the time spent and resets the start time
timer.intervalMinute();// It takes minutes

Copy the code

Group timing can also be achieved:

final TimeInterval timer = new TimeInterval();

/ / group 1
timer.start("1");
ThreadUtil.sleep(800);

/ / group 2
timer.start("2");
ThreadUtil.sleep(900);

Console.log("Timer 1 took {} ms", timer.intervalMs("1"));
Console.log("Timer 2 took {} ms", timer.intervalMs("2"));
Copy the code