StopWatch 监控时间用法
// stopWatch 引入的jar包 org.springframework.util.StopWatch
// start 与 stop 是一一对应的, 如果想要在start与 stop中监控其他内容耗时,此时需要重新创建一个对象.
// 不允许在同一个对象中连续start 两次, 否则会抛出异常(IllegalStateException)
try{
Long sTime = System.currentTimeMillis();
StopWatch sw = new StopWatch();
sw.start("for循环");
StringBuffer a = new StringBuffer();
for (int i = 0; i < 10000000; i++){
a = a.append(i);
if(i == 10000){
StopWatch sw1 = new StopWatch();
sw1.start("5000");
Thread.sleep(5000);
sw1.stop();
System.out.println(sw1.prettyPrint());
}
}
sw.stop();
sw.start("for循环2");
StringBuffer b = new StringBuffer();
for (int i = 0; i < 10000000; i++){
b = b.append(i);
if(i == 10000){
Thread.sleep(1000);
}
}
sw.stop();
Long eTime = System.currentTimeMillis();
System.out.println("共耗时 : " + (eTime - sTime) + " 毫秒;");
System.out.println(sw.prettyPrint());
}catch (Exception e){
}