Day 8:性能优化
性能优化可以使我们的应用程序更快、更稳定。以下是一个简单的Java程序,可以帮助我们测量和优化代码性能:
javaCopy Codeimport java.util.ArrayList;
import java.util.List;
public class PerformanceTest {
private static final int TEST_SIZE = 1000000;
public static void main(String[] args) {
// 创建测试数据
List<Integer> testData = new ArrayList<>();
for (int i = 0; i < TEST_SIZE; i++) {
testData.add(i);
}
// 测试for循环迭代
long startTime1 = System.currentTimeMillis();
for (int i = 0; i < testData.size(); i++) {
int value = testData.get(i);
}
long endTime1 = System.currentTimeMillis();
System.out.println("for循环迭代时间:" + (endTime1 - startTime1) + "ms");
// 测试增强型for循环迭代
long startTime2 = System.currentTimeMillis();
for (int value : testData) {
}
long endTime2 = System.currentTimeMillis();
System.out.println("增强型for循环迭代时间:" + (endTime2 - startTime2) + "ms");
}
}
标签:迭代,int,System,long,currentTimeMillis,冲刺,testData From: https://www.cnblogs.com/2574999647wyx/p/17473099.html