62,621
社区成员
发帖
与我相关
我的任务
分享

public static void f1() {
String localFolder1 = "F:\\test";
File file = new File(localFolder1);
File[] templist = file.listFiles();
for (int i = 0; i < templist.length; i++) {
if (templist[i].isDirectory()) {
String templocalFolder = templist[i] + "";
File file2 = new File(templocalFolder);
File[] templist2 = file2.listFiles();
if (templist2.length > 0) {
for (int j = 0; j < templist2.length; j++) {
if (j % 2 == 0) {
continue;
}
int t = 0;
}
}
}
}
}
public static void f2() {
String localFolder1 = "F:\\test";
File file = new File(localFolder1);
File[] templist = file.listFiles();
for (int i = 0; i < templist.length; i++) {
if (templist[i].isDirectory()) {
String templocalFolder = templist[i] + "";
File file2 = new File(templocalFolder);
File[] templist2 = file2.listFiles();
if (templist2.length > 0) {
for (int j = 0; j < templist2.length; j++) {
if (!(j % 2 == 0)) {
int t = 0;
}
}
}
}
}
}
public static void main(String[] args) {
// f2 在前
long time1 = System.nanoTime();
f2();
long useTime1 = System.nanoTime() - time1;
System.out.println("f2:" + useTime1);
long time2 = System.nanoTime();
f1();
long useTime2 = System.nanoTime() - time2;
System.out.println("f1:" + useTime2);
// // f1 在前
// long time1 = System.nanoTime();
// f1();
// long useTime1 = System.nanoTime() - time1;
// System.out.println("f1:" + useTime1);
// long time2 = System.nanoTime();
// f2();
// long useTime2 = System.nanoTime() - time2;
// System.out.println("f2:" + useTime2);
}
你自己切换下,注释的代码, 就知道是什么原因了public static void f1() {
String localFolder1 = "F:\\test";
File file = new File(localFolder1);
File[] templist = file.listFiles();
for (int i = 0; i < templist.length; i++) {
if (templist[i].isDirectory()) {
String templocalFolder = templist[i] + "";
File file2 = new File(templocalFolder);
File[] templist2 = file2.listFiles();
if (templist2.length > 0) {
for (int j = 0; j < templist2.length; j++) {
int t = 0;
}
}
}
}
}
public static void f2() {
String localFolder1 = "F:\\test";
File file = new File(localFolder1);
File[] templist = file.listFiles();
for (int i = 0; i < templist.length; i++) {
if (templist[i].isDirectory()) {
String templocalFolder = templist[i] + "";
File file2 = new File(templocalFolder);
File[] templist2 = file2.listFiles();
if (templist2.length > 0) {
for (int j = 0; j < templist2.length; j++) {
int t = 0;
}
}
}
}
}
public static void main(String[] args) {
// f2 在前
// long time1 = System.nanoTime();
// f2();
// long useTime1 = System.nanoTime() - time1;
// System.out.println("f2:" + useTime1);
// long time2 = System.nanoTime();
// f1();
// long useTime2 = System.nanoTime() - time2;
// System.out.println("f1:" + useTime2);
// f1 在前
long time1 = System.nanoTime();
f1();
long useTime1 = System.nanoTime() - time1;
System.out.println("f1:" + useTime1);
long time2 = System.nanoTime();
f2();
long useTime2 = System.nanoTime() - time2;
System.out.println("f2:" + useTime2);
}
从代码的结果可以看得出来,和continue没有关系,.产生该情况的原因,是和先后读取文件有关系.应该是jvm做了缓存.具体还需要进一步查看相关的字节码.[/quote]
我是读取一样的文件,这两个分别执行然后得出的时间差。而且不止执行一次。没用continue的慢很多。代码如图1楼图片所示。下面也有贴代码
//模拟操作
private static void opt() {
List<Integer> aIntegers = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
aIntegers.add(i);
}
}
//use if
public static void f1(List<Integer> aList){
for (int i = 0; i < aList.size(); i++) {
if(aList.get(i) % 15 == 0){
opt();
}
}
}
//use continue
public static void f2(List<Integer> aList) {
for (int i = 0; i < aList.size(); i++) {
if (!(aList.get(i) % 15 == 0)) {
continue;
}else {
opt();
}
}
}
public static void main(String[] args) {
List<Integer> aList = new ArrayList<>();
for (int i = 0; i < 1000000; i++) {
aList.add(i);
}
//测试
for (int i = 0; i < 10; i++) {
long time1 = System.nanoTime();
f1(aList);
long useTime1 = System.nanoTime() - time1;
System.out.println("f1:" + useTime1);
long time2 = System.nanoTime();
f2(aList);
long useTime2 = System.nanoTime() - time2;
System.out.println("f2:" + useTime2);
}
}
测试结果;并不存在,相关性能问题. 楼主的现象可能是代码逻辑产生的偶然现象.