赞
踩
最近经常滑水,没事就看看开发代码,又发现一处之前没接触过的知识(哎,学习是无止境的),发现了Profilers的用法。
摘抄于百度:阶段性的记录日志,并打印出各个阶段所耗费的时间,可用于性能分析之类的
说明:
1.引入依赖,看下面例子
2.new一个Profiler
3.调用start方法,开始进行性能记录
4.调用实际要运行的方法
5.重复3和4步骤
6.调用stop方法,打印profiler的内容(下面输出的Profiler【Sample】内容)
在执行该例子前,要在依赖中导入
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-ext</artifactId>
- <version>1.7.25</version>
- </dependency>
以下例子摘抄于网络:https://my.oschina.net/u/160225/blog/4691386
- import org.slf4j.profiler.Profiler;
- import org.slf4j.profiler.TimeInstrument;
-
- /**
- * @Author: 一片蓝蓝的云
- * @Date: 2021/4/7
- */
- public class ProfilerExample {
- public void demoMethod1(){
- double sum = 0;
- for(int i=0; i< 1000; i++){
- sum = sum+(Math.pow(i, 2));
- }
- System.out.println("Sum of squares of the numbers from 1 to 10000: "+sum);
- }
-
- public void demoMethod2(){
- int sum = 0;
- for(int i=0; i< 10000; i++){
- sum = sum+i;
- }
- System.out.println("Sum of the numbers from 1 to 10000: "+sum);
- }
-
- public static void main(String[] args) {
- ProfilerExample obj = new ProfilerExample();
- //Creating a profiler
- Profiler profiler = new Profiler("Sample");
- //Starting a child stop watch and stopping the previous one.
- profiler.start("Task 1");
- obj.demoMethod1();
- //Starting another child stop watch and stopping the previous one.
- profiler.start("Task 2");
- obj.demoMethod2();
- //Stopping the current child watch and the global watch.
- TimeInstrument tm = profiler.stop();
- //printing the contents of the time instrument
- tm.print();
- }
- }

以下是执行情况
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。