1、一般我们检测某段代码执行的时间,都是以如下方式来进行的:
public static void main(String[] args) {
long startTime=System.currentTimeMillis(); //获取开始时间
//函数主体代码
long endTime=System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
}
2、Guava中com.google.common.base.Stopwatch使用:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import com.google.common.base.Stopwatch;
public class GuavaTest {
@Test
public void testStopwatch() throws InterruptedException {
// 创建自动start的计时器
Stopwatch watch = Stopwatch.createSt