看完https://blog.csdn.net/wh_forever/article/details/79203338这篇博客,实际动手测试,发现了一些问题,代码的编写果然充满了魅力,自己不试试哪能知道到底怎么样,建议大家也试试,平时注意代码编写的细节可以让我们编写更优秀的代码。
本机软件环境,JDK1.8 IDEA2019.2
1、嵌套循环
package cn.waggag.performance;
/**
* @description:
* @author: waggag
* @time: 2019/7/30 19:42
* @Company http://www.waggag.cn
*/
public class Test {
private static long startTime;
private static long endTime;
public static void main(String[] args) {
/**
* Returns the current value of the running Java Virtual Machine's
* high-resolution time source, in nanoseconds.
* nanoTime()
* 返回最准确的可用系统计时器的当前值,以毫微秒为单位。
*/
//优化前
startTime = System.nanoTime();
for (int i = 0; i <10 ; i++) {
for (int j = 0; j < 1000000000; j++) {
}
}
endTime = System.nanoTime();
System.out.println("外小内大耗时:"+(endTime - startTime));
//优化后
startTime = System.nanoTime();
for (int i = 0; i < 1000000000; i++) {
for (int j = 0; j < 10; j++) {
}
}
endTime =