关于在for循环判断使用continue速度的区别,求解

kkoo301 2017-01-14 09:22:24
如题求解,为什么第二个代码比第第一个代码快了几倍,执行的东西都是一样的,求详解。另外代码求帮忙指点优化下

...全文
1566 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
huitailang_1_1 2020-12-11
  • 打赏
  • 举报
回复
continue确实会执行时间长很多,不知道是为什么,可能要到汇编层面才能看清楚
  • 打赏
  • 举报
回复
楼主 你把continue 缓存break试下,两者运行时间估计差不多。brake的作用是跳出此次循环继续往下执行,而continue就直接结束,执行下一次循环。
  • 打赏
  • 举报
回复
假如这两个方法同时执行的话有 continue进程会直接结束掉释放资源进行下次循环,而没有continue会占有进程执行完才被释放,所以有continue执行的速度相对快。 个人理解、、
搬砖生涯 2017-01-24
  • 打赏
  • 举报
回复
引用 33 楼 yu16568316 的回复:
你看一下你第一个里面的templist2[j].isFile()这个判断,isFile()去看源码看看都干了些什么。这个方法才是费时的东西,跟continue没关系
第二个方法判断isFile就直接continue了,当然快了
搬砖生涯 2017-01-24
  • 打赏
  • 举报
回复
你看一下你第一个里面的templist2[j].isFile()这个判断,isFile()去看源码看看都干了些什么。这个方法才是费时的东西,跟continue没关系
kkoo301 2017-01-18
  • 打赏
  • 举报
回复
引用 30 楼 gzcitizeny 的回复:
感觉是两个if语句执行效率不一样,而且在循环内,符合if(!df.format(calendar.getTime()).equals(UploadFileToHadoop.subDF(templist2[j].getName())))此语句并执行continue的占大多数。
continue的文件确实比较多,但是这样不也一样遍历了文件夹吗。我用第一种方法把为真的往下执行到底有啥不同呢。一直 不明白这点而已。
kkoo301 2017-01-18
  • 打赏
  • 举报
回复
引用 28 楼 hspingcc 的回复:
[quote=引用 26 楼 kkoo301 的回复:] [quote=引用 25 楼 hspingcc 的回复:]


	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);
	}

你自己切换下,注释的代码, 就知道是什么原因了
但是我的代码没用这种情况啊,无论他是否在前面都是很慢。是不是我的第一种种方法写的有问题。[/quote] 抛开你的代码的具体内容来分析,第一 continue没有性能的优越性.第二.操作文件的时候存在着先操作比后操作慢很多的现象,即为jvm或者系统做了缓存机制. 排除这写问题,你就可以检查你自己的问题了[/quote] 仔细说下我的需求吧,我是遍历磁盘,然后遍历文件夹把时间时前一个小时的文件上传到某个服务器,文件夹的文件都是实时更新的我只要把最新的上传上去就是了,就如你看的我写的代码样。你说的那种问题我现在放在 不同的项目里还是会出现速度方面的问题。
insping 2017-01-17
  • 打赏
  • 举报
回复


	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);
	}

你自己切换下,注释的代码, 就知道是什么原因了
kkoo301 2017-01-17
  • 打赏
  • 举报
回复
引用 22 楼 hspingcc 的回复:
原因不是continue,.是因为jvm在第一次读取的时候会建立缓存,第二次就会比较的快.
我这两个代码都先后执行了,咋可能是你说的那个原因啊
kkoo301 2017-01-17
  • 打赏
  • 举报
回复
引用 22 楼 hspingcc 的回复:
原因不是continue,.是因为jvm在第一次读取的时候会建立缓存,第二次就会比较的快.
我这两个代码都先后执行了,咋可能是你说的那个原因啊
insping 2017-01-17
  • 打赏
  • 举报
回复
原因不是continue,.是因为jvm在第一次读取的时候会建立缓存,第二次就会比较的快.
kkoo301 2017-01-17
  • 打赏
  • 举报
回复
引用 20 楼 hspingcc 的回复:
[quote=引用 15 楼 kkoo301 的回复:] [quote=引用 14 楼 hspingcc 的回复:]

	
	//模拟操作
	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);
		}
	}

测试结果;并不存在,相关性能问题. 楼主的现象可能是代码逻辑产生的偶然现象.
但是我现在就存在这种问题啊,如果不continue非常慢。我的是遍历文件夹然后遍历文件。能看看我代码具体啥原因不[/quote]
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楼图片所示。下面也有贴代码
gzcitizeny 2017-01-17
  • 打赏
  • 举报
回复
感觉是两个if语句执行效率不一样,而且在循环内,符合if(!df.format(calendar.getTime()).equals(UploadFileToHadoop.subDF(templist2[j].getName())))此语句并执行continue的占大多数。
斯卡洛特 2017-01-17
  • 打赏
  • 举报
回复
可以把判断条件分开测试,或者断点执行。看看哪里卡顿的时间比较长
insping 2017-01-17
  • 打赏
  • 举报
回复
引用 26 楼 kkoo301 的回复:
[quote=引用 25 楼 hspingcc 的回复:]


	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);
	}

你自己切换下,注释的代码, 就知道是什么原因了
但是我的代码没用这种情况啊,无论他是否在前面都是很慢。是不是我的第一种种方法写的有问题。[/quote] 抛开你的代码的具体内容来分析,第一 continue没有性能的优越性.第二.操作文件的时候存在着先操作比后操作慢很多的现象,即为jvm或者系统做了缓存机制. 排除这写问题,你就可以检查你自己的问题了
yuqi_hz 2017-01-17
  • 打赏
  • 举报
回复
慢的那个多了一个ISFILE的判断
kkoo301 2017-01-17
  • 打赏
  • 举报
回复
引用 25 楼 hspingcc 的回复:


	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);
	}

你自己切换下,注释的代码, 就知道是什么原因了
但是我的代码没用这种情况啊,无论他是否在前面都是很慢。是不是我的第一种种方法写的有问题。
kkoo301 2017-01-16
  • 打赏
  • 举报
回复
引用 14 楼 hspingcc 的回复:

	
	//模拟操作
	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);
		}
	}

测试结果;并不存在,相关性能问题. 楼主的现象可能是代码逻辑产生的偶然现象.
但是我现在就存在这种问题啊,如果不continue非常慢。我的是遍历文件夹然后遍历文件。能看看我代码具体啥原因不
insping 2017-01-16
  • 打赏
  • 举报
回复

	
	//模拟操作
	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);
		}
	}

测试结果;并不存在,相关性能问题. 楼主的现象可能是代码逻辑产生的偶然现象.
kkoo301 2017-01-16
  • 打赏
  • 举报
回复
引用 12 楼 liu1324457514 的回复:
是不是因为声明 String tempRemot2=tempRemot+templist2[j].getName(); String templocalFolder2=templist2[j]+"";的原因,你第一种方法每次循环都在进行判断之前先声明两个变量而第二个方法是在判断结果为真后再声明
我把這些丟出去也是很慢,沒搞懂原因
加载更多回复(17)

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧