SpringBoot statistics code execution takes time, and there are so many ways to play!

Click on the “Java base” above, select “Set as star”

Be a positive person, not a positive waste person!

Update articles every day at 14:00, lose a million bits of hair every day…

Source code boutique column

  • Original | Java 2021 Super God Road, very liver~

  • An open source project with detailed annotations in Chinese

  • RPC framework Dubbo source code analysis

  • Network application framework Netty source code analysis

  • Message middleware RocketMQ source code analysis

  • Database middleware Sharding-JDBC and MyCAT source code analysis

  • Job Scheduling Middleware Elastic-Job Source Code Analysis

  • Distributed transaction middleware TCC-Transaction source code analysis

  • Eureka and Hystrix source code analysis

  • Java Concurrency Source Code

Source: blog.csdn.net/qq_35387940/

article/details/128444074

  • foreword

  • text

    • ① StopWatch

    • ② System.nanoTime()

    • ③new Date()

    • ④ System. currentTimeMillis()

Foreword

Recently, a newcomer in the group mentioned about counting code execution time:

5733f89580e40f37c704de0cc23925b2.png

Start System.currentTimeMillis() minus end System.currentTimeMillis() equals elapsed time

In fact, I personally feel that it is OK, and it is quite good as it is, and many projects are used in this way.

Simple is fine.

10761df86911edd69db5fd42062fe03b.png

This article is just to talk about other ways to play. I appreciate this guy’s thirst for knowledge. It’s okay to learn more about it.

[Key] ps: Three by analogy has additional meanings, that is, three consecutive, likes, collections, and attention.

Background management system + user applet based on Spring Boot + MyBatis Plus + Vue & amp; Element, supports RBAC dynamic permissions, multi-tenancy, data permissions, workflow, three-party login, payment, SMS, mall and other functions

  • Project address: https://github.com/YunaiV/ruoyi-vue-pro

  • Video tutorial: https://doc.iocoder.cn/video/

Text

① StopWatch

The first way to play, StopWatch provided in spring util

Sample code:

StopWatch stopWatch = new StopWatch();
stopWatch. start();
//doInsert();
//Execute business, etc.
stopWatch. stop();
System.out.println(stopWatch.getTotalTimeMillis());

Effect:

741b2618a36f5ae1172972fad24e28e2.png

② System.nanoTime()

The second way to play System.nanoTime()

Don’t worry about how to use it. When we read the first StopWatch, do you have enough thoughts from your friends, thinking about the time-consuming statistics of the spring package, how did you realize it?

Digression:

We must develop this kind of emanating thinking. Many brothers and friends have given me feedback on some topics, that is to say, there is nothing to learn in the project.

In fact, this is very normal. The working process is not a teaching process. You need to have a learning mindset of “vertical excavation” and horizontal “distribution”.

Just click on the source code of StopWatch to take a look, oh, the principle is to use System.nanoTime() :

03701da84ba640387499419af66880b4.png

Example of System.nanoTime() code usage:

long startTime = System.nanoTime();
doInsert();
//execute business
long endTime = System. nanoTime();
System.out.println((endTime - startTime));

Effect :

f08722b069f30d4fda44a3fa12e0b11e.png

③ new Date()

The third way to play is that I occasionally see others write new Date like this

Sample code:

Date startDate = new Date();
// doInsert();
//Execute business, etc.
 Date endDate = new Date();
 System.out.println((endDate.getTime() - startDate.getTime()));

Effect:

a3c6f7724c28206d8d2b382cb42cf441.png

④ System. currentTimeMillis()

to omit

ps: StopWatch is actually more than just blocking the time-consuming statistics, which is too much. . up.

It actually encapsulates a lot of other functions related to time statistics (if you are interested, you can study it separately, especially refer to the author’s packaging ideas):

  • void start("task name"): start the timing of a task name

  • void stop(): Stop the timing of the current task

  • boolean isRunning(): whether a task is being timed

  • long getTotalTimeMillis(): the total execution time of all tasks (in milliseconds)

  • double getTotalTimeSeconds(): the total time of all tasks (in seconds)

  • long getLastTaskTimeMillis(): the time spent on the last task (in milliseconds)

  • int getTaskCount(): the number of scheduled tasks

  • String prettyPrint(): beautifully print the detailed time-consuming situation of all tasks

  • StopWatch.TaskInfo[] getTaskInfo(): An array of entity classes containing task names and task time consumption

Well, that’s all for this article.

Welcome to join my knowledge planet, discuss architecture and exchange source code together. How to join, Long press the QR code below:

5c2570531c848132d5721023b3c9b6d6.png

The source code has been updated on Knowledge Planet and the analysis is as follows:

345c0e1b1019bba64e49cf3cdebd382d.jpeg

b2dc197c9bce4ae1cda0efd8b3846f0f.jpeg

fa44982f3637304377a45109ae894d4b.jpeg

047a5876d1ffadd77b5276c5d57ac9e9.jpeg

The recently updated series “Introduction to Taro SpringBoot 2.X” has more than 101 articles, covering MyBatis, Redis, MongoDB, ES, sub-database and sub-table, read-write separation, SpringMVC, Webflux, permissions, WebSocket, Dubbo, RabbitMQ, RocketMQ , Kafka, performance testing, etc.

Provides a SpringBoot example with nearly 3W lines of code, and an e-commerce microservice project with more than 6W lines of code.

How to get it: Click “Looking“, follow the official account and reply to 666 to receive, more content will be provided one after another.

If the article is helpful, please read it and forward it.
Thank you for your support (*^__^*)