java 测试程序运行时间 一分钟学会 简单易上手

第一种:

                long start=System.currentTimeMillis();
		Scanner in=new Scanner(System.in);
		BigDecimal n=in.nextBigDecimal();
		in.close();
		System.out.println(n+"!=:"+factorial(n));
		long end=System.currentTimeMillis();
		System.out.println("当前程序运行了:"+(end-start)+"ms");

这种是毫秒级别的,分别再刚开始和最后声明两个long型变量,相减即可。

第二种:

long start=System.nanoTime();
		Scanner in=new Scanner(System.in);
		BigDecimal n=in.nextBigDecimal();
		in.close();
		System.out.println(n+"!=:"+factorial(n));
		long end=System.nanoTime();
		System.out.println("当前程序运行了:"+(end-start)+"ns");

跟第一种雷同。

猜你喜欢

转载自blog.csdn.net/qq_41325698/article/details/88777308