for循环

js各种for循环的效率问题

微笑、不失礼 提交于 2019-12-09 05:59:44
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> </style> </head> <body> <script> let newarr = new Array(100000) let myfor = [] let myforin = [] let myforeach = [] let myforof = [] let mymap = [] let myfind = [] let myfilter = [] let mywhile = [] let mysome = [] let myevery = [] console.time("forin"); for(x in newarr){ myforin.push(x) } console.timeEnd("forin") console.time("for"); for(let i=0;i<newarr.length;i++){ myfor.push

PHP 中 foreach和for循环哪个效率更高

吃可爱长大的小学妹 提交于 2019-12-09 05:59:32
今天看别人的代码,有段代码是对一个二维数组中的数据进行处理,那个作者用的是for循环,但是我感觉用 foreach 也可以。所以就想问问看在PHP中 for 循环和 foreach 哪个更快。 for循环遍历(count在内部): $big_Array = range(0,1000000,1); $start_For_Time = microtime_float(); for ($i=0;$i<count($big_Array);$i++) { $i; } $end_For_Time = microtime_float(); $for_Time = $end_For_Time - $start_For_Time; echo 'for循环遍历耗时:'.$for_Time.'<br>'; //for循环遍历(count在内部)耗时:0.039999961853027 for循环遍历(count在外部): $big_Array = range(0,1000000,1); $start_For_Time = microtime_float(); $array_Count = count($big_Array); for ($i=0;$i<$array_Count;$i++) { $i; } $end_For_Time = microtime_float(); $for_Time =

foreach 和 for 循环的区别

爱⌒轻易说出口 提交于 2019-12-09 05:58:05
foreach语句是java5的新特征之一,在遍历数组、集合方面,foreach为开发人员提供了极大的方便。 foreach语句是for语句的特殊简化版本,但是foreach语句并不能完全取代for语句,然而,任何的foreach语句都可以改写为for语句版本。 foreach并不是一个关键字,习惯上将这种特殊的for语句格式称之为“foreach”语句。 从英文字面意思理解foreach也就是“for 每一个”的意思。实际上也就是这个意思。 foreach的语句格式: for(元素类型t 元素变量x : 遍历对象obj){ 引用了x的java语句; } 一、foreach简化数组和集合的遍历 下面通过两个例子简单例子看看foreach是如何简化编程的。代码如下: import java.util.Arrays; import java.util.List; import java.util.ArrayList; /** * Created by IntelliJ IDEA. * User: leizhimin * Date: 2007-12-3 * Time: 16:58:24 * Java5新特征之foreach语句使用总结 */ public class TestArray { public static void main(String args[]) {

在C++中实现foreach循环,比for_each更简洁!

霸气de小男生 提交于 2019-12-09 05:50:44
python,c#,java里面都有类似于foreach的结构,stl里面虽然有for_each这个函数,但是感觉使用还是太繁琐了一些,所以就自己实现了一个。 先来看看stl里面的for_each函数,官方文档上的原型如下: 1 Function for_each ( InputIterator first , InputIterator last , Function f ) ; 示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // for_each example #include <iostream> #include <algorithm> #include <vector> using namespace std ; void myfunction ( int i ) { cout << " " << i ; } struct myclass { void operator ( ) ( int i ) { cout << " " << i ; } } myobject ; int main ( ) { vector < int > myvector ; myvector . push_back ( 10 ) ; myvector .

对数据遍历的三种方式之foreach 、for与Iterator

给你一囗甜甜゛ 提交于 2019-12-09 05:49:38
对数据的遍历方式有三种: for 循环遍历、 增强 for 循环遍历、 Iterator 迭代器遍历。这三种遍历方式有他们自身使用的范围。 增强 for 循环 增强 for 循环是 JDK1.5 版本后出来的一个高级 for 循环,是用来专门遍历数组和集合的。内部原理其实是一个迭代器 遍历也就是我们常说的 foreach 遍历。 格式: for (元素的数据类型 变量 : 集合 or 数组) 应用一: 利用常用的for循环遍历数组 : //利用传统方式进行遍历 int[] arr={1,2,3,4,5}; for (int i = 0; i < arr.length; i++) { System.out.println(i); } 有了增强for循环后,方便了我们进行遍历 int[] arr = new int[]{11,22,33}; for (int n : arr) {//变量n代表被遍历到的数组元素 System.out.println(n); } for (int n : arr) //变量n代表被遍历到的数组元素,类型是int 变量类型可以是n也可以是自己定义的任意变量名 原理:我们是将数组中的元素给了n,然后对n进行输出。 增强for循环和for循环的区别: 1.增强for循环只能遍历集合和数组,for循环最基本的遍历方式,除了集合和数组也可以用于其他遍历 2

java ArrayList与LinkedList 使用for,forearch,Iterator的遍历效率

被刻印的时光 ゝ 提交于 2019-12-09 05:47:41
public class test { /** * @param args */ public static void main(String[] args) { ArrayList<Integer> al=new ArrayList<Integer>(); for(int i=0;i<900000;i++){ al.add(new Integer(4)); } showForForInter(al); showFor(al); showForEach(al); showIterator(al); } //for int 循环遍历 public static void showForForInter(ArrayList<Integer> al){ int no=al.size(); long start =System.nanoTime(); for(int j=0;j<no;j++){ al.get(j); } long end =System.nanoTime(); System.out.println("for int 循环遍历:"+(end- start) / (1000)); } //for循环遍历 public static void showFor( ArrayList<Integer> al){ //开始编译执行时间 long start =System

迭代器Iterator与for循环的区别

冷暖自知 提交于 2019-12-09 05:46:29
迭代器是一种模式,它可以使得对于序列类型的数据结构的遍历行为与被遍历的对象分离,即我们无需关心该序列的底层结构是什么样子的。只要拿到这个对象,使用迭代器就可以遍历这个对象的内部. 1.Iterator Java提供一个专门的迭代器<<interface>>Iterator,我们可以对某个序列实现该interface,来提供标准的Java迭代器。Iterator接口实现后的功能是“使用”一个迭代器. 文档定义: [java] view plain copy print ? Package java.util; public interface Iterator<E> { boolean hasNext(); //判断是否存在下一个对象元素 E next(); void remove(); } [java] view plain copy print ? <span style= "font-size:18px;" >Package java.util; public interface Iterator<E> { boolean hasNext(); //判断是否存在下一个对象元素 E next(); void remove(); } </span> 2.Iterable Java中还提供了一个Iterable接口,Iterable接口实现后的功能是“返回”一个迭代器

迭代器和for循环的效率比较

我是研究僧i 提交于 2019-12-09 05:45:29
实体Person类 package com.zhang.loop; public class Person { private String a; private int b; public Person(String a, int b) { super(); this.a = a; this.b = b; } public String getA() { return a; } public void setA(String a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } } 测试ArrayList的主程序 package com.zhang.loop; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class TestArrayList { private static final int COUNT = 800000; private static List<Person> persons = new ArrayList<Person>(); public

关于for循环和Iterator遍历ArrayList的性能问题

徘徊边缘 提交于 2019-12-09 05:44:30
今日看到@DriveMan的一篇博客,题为《 ArrayList集合实现RandomAccess接口有何作用?为何LinkedList集合却没实现这接口? 》,文中提到对于实现了 RandomAccess 接口的类来说,使用for循环遍历比使用Iterator遍历更加高效快速。 由于本人之前没了解过这方面的知识,阅此博客后心怀好奇便去查阅了API文档了解了一下,官网的介绍是这样的: Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists. 上面这段话的意思就是:RandomAccess是一个标记接口,用于标明实现了该接口的List支持快速随机访问。并且指出了该接口的主要用途是当随机或顺序访问一些List时,允许泛型算法改变它们的行为来提升性能。

对比C#中for和foreach循环的性能

坚强是说给别人听的谎言 提交于 2019-12-09 05:42:10
大家先来看看如下三个循环: int [] foo = new int[100]; 1, foreach (int i in foo) Console.WriteLine(i.ToString()); 2,for(int index=0;index Console.WriteLine(foo[index].ToString()); 3,int len=foo.Length; for(int index=0;index Console.WriteLine(foo[index].ToString());    这三个循环是我在看《Effective C#》中看到的,发现书中说第三个循环和如下代码等效,经过使用ILDasm.exe工具查看IL代码发现这个说法并不正确: int len=foo.Length; for(int index=0;index { if(index Console.WriteLine(foo[index].ToString()); else throw new IndexOutOfRangeException(); }   书中的看法是数组的边界测试会被执行两次(编译器生成的代码一次,JIT编译阶段还要执行一次检查),但是的确没有在IL代码中发现C#的编译器生成类似的逻辑,所以这个说法有问题!   一般C++转过来的程序员都很喜欢这样写循环