どちらが速いですか? foreach vs. List.ForEachと forループ

今日( 翻訳者のメモ: 6年前)、foreachコンストラクトを使用してリストをソートし、少し満足して、それがどれほど効率的で、何であるかを認識して、ArrayListを使用してみました。 Genericの奇跡のおかげで、C#コンパイラSystem.Collections.Generic.IEnumerator System.Collections.IEnumerator . : " ?" , , , , .



NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

:

long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






C# IL , :

long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





, C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

, :

long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








, List.ForEach , Action<>



, . callvirt IL , , callvirt. , , List.ForEach .

, , List :

for (int i = 0; i < List.Count; i++)






for (int i = 0; i < NUM_ITEMS; i++)





foreach (int i in List)





List.ForEach(delegate(int i) { result += i; });







, :

画像

. , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

, , " ":

画像

, , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



, Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

, , . 5 , 10000000 50000000 .



:

画像



:

画像



: ForEach List, . Array.ForEach




( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



.



:

.. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







, , , 1000 . 1000 , . , , .

:

画像

, . ForEach .




































































インスタンスを使用して、多数のパッケージ化操作をきちんと回避しますSystem.Collections.Generic.IEnumerator System.Collections.IEnumerator



. : " ?" , , , , .



NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

:

long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






C# IL , :

long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





, C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

, :

long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








, List.ForEach , Action<>



, . callvirt IL , , callvirt. , , List.ForEach .

, , List :

for (int i = 0; i < List.Count; i++)






for (int i = 0; i < NUM_ITEMS; i++)





foreach (int i in List)





List.ForEach(delegate(int i) { result += i; });







, :

画像

. , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

, , " ":

画像

, , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



, Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

, , . 5 , 10000000 50000000 .



:

画像



:

画像



: ForEach List, . Array.ForEach




( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



.



:

.. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







, , , 1000 . 1000 , . , , .

:

画像

, . ForEach .
































































System.Collections.Generic.IEnumerator System.Collections.IEnumerator



. : " ?" , , , , .



NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

:

long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






C# IL , :

long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





, C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

, :

long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








, List.ForEach , Action<>



, . callvirt IL , , callvirt. , , List.ForEach .

, , List :

for (int i = 0; i < List.Count; i++)






for (int i = 0; i < NUM_ITEMS; i++)





foreach (int i in List)





List.ForEach(delegate(int i) { result += i; });







, :

画像

. , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

, , " ":

画像

, , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



, Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

, , . 5 , 10000000 50000000 .



:

画像



:

画像



: ForEach List, . Array.ForEach




( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



.



:

.. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







, , , 1000 . 1000 , . , , .

:

画像

, . ForEach .
































































 System.Collections.Generic.IEnumerator   System.Collections.IEnumerator
      
      



. : " ?" , , , , .



NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

:

long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






C# IL , :

long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





, C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

, :

long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








, List.ForEach , Action<>



, . callvirt IL , , callvirt. , , List.ForEach .

, , List :

for (int i = 0; i < List.Count; i++)






for (int i = 0; i < NUM_ITEMS; i++)





foreach (int i in List)





List.ForEach(delegate(int i) { result += i; });







, :

画像

. , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

, , " ":

画像

, , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



, Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

, , . 5 , 10000000 50000000 .



:

画像



:

画像



: ForEach List, . Array.ForEach




( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



.



:

.. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







, , , 1000 . 1000 , . , , .

:

画像

, . ForEach .
































































System.Collections.Generic.IEnumerator System.Collections.IEnumerator



. : " ?" , , , , .



NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

:

long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






C# IL , :

long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





, C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

, :

long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








, List.ForEach , Action<>



, . callvirt IL , , callvirt. , , List.ForEach .

, , List :

for (int i = 0; i < List.Count; i++)






for (int i = 0; i < NUM_ITEMS; i++)





foreach (int i in List)





List.ForEach(delegate(int i) { result += i; });







, :

画像

. , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

, , " ":

画像

, , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



, Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

, , . 5 , 10000000 50000000 .



:

画像



:

画像



: ForEach List, . Array.ForEach




( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



.



:

.. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







, , , 1000 . 1000 , . , , .

:

画像

, . ForEach .
































































 System.Collections.Generic.IEnumerator   System.Collections.IEnumerator
      
      



. : " ?" , , , , .



NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

:

long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






C# IL , :

long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





, C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

, :

long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








, List.ForEach , Action<>



, . callvirt IL , , callvirt. , , List.ForEach .

, , List :

for (int i = 0; i < List.Count; i++)






for (int i = 0; i < NUM_ITEMS; i++)





foreach (int i in List)





List.ForEach(delegate(int i) { result += i; });







, :

画像

. , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

, , " ":

画像

, , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



, Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

, , . 5 , 10000000 50000000 .



:

画像



:

画像



: ForEach List, . Array.ForEach




( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



.



:

.. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







, , , 1000 . 1000 , . , , .

:

画像

, . ForEach .
































































System.Collections.Generic.IEnumerator System.Collections.IEnumerator



. : " ?" , , , , .



NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

:

long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






C# IL , :

long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





, C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

, :

long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








, List.ForEach , Action<>



, . callvirt IL , , callvirt. , , List.ForEach .

, , List :

for (int i = 0; i < List.Count; i++)






for (int i = 0; i < NUM_ITEMS; i++)





foreach (int i in List)





List.ForEach(delegate(int i) { result += i; });







, :

画像

. , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

, , " ":

画像

, , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



, Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

, , . 5 , 10000000 50000000 .



:

画像



:

画像



: ForEach List, . Array.ForEach




( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



.



:

.. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







, , , 1000 . 1000 , . , , .

:

画像

, . ForEach .
































































 System.Collections.Generic.IEnumerator   System.Collections.IEnumerator
      
      



. : " ?" , , , , .



NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

:

long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






C# IL , :

long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





, C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

, :

long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








, List.ForEach , Action<>



, . callvirt IL , , callvirt. , , List.ForEach .

, , List :

for (int i = 0; i < List.Count; i++)






for (int i = 0; i < NUM_ITEMS; i++)





foreach (int i in List)





List.ForEach(delegate(int i) { result += i; });







, :

画像

. , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

, , " ":

画像

, , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



, Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

, , . 5 , 10000000 50000000 .



:

画像



:

画像



: ForEach List, . Array.ForEach




( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



.



:

.. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







, , , 1000 . 1000 , . , , .

:

画像

, . ForEach .
































































System.Collections.Generic.IEnumerator System.Collections.IEnumerator



. : " ?" , , , , .



NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

:

long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






C# IL , :

long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





, C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

, :

long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








, List.ForEach , Action<>



, . callvirt IL , , callvirt. , , List.ForEach .

, , List :

for (int i = 0; i < List.Count; i++)






for (int i = 0; i < NUM_ITEMS; i++)





foreach (int i in List)





List.ForEach(delegate(int i) { result += i; });







, :

画像

. , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

, , " ":

画像

, , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



, Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

, , . 5 , 10000000 50000000 .



:

画像



:

画像



: ForEach List, . Array.ForEach




( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



.



:

.. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







, , , 1000 . 1000 , . , , .

:

画像

, . ForEach .
































































  1. System.Collections.Generic.IEnumerator System.Collections.IEnumerator



    . : " ?" , , , , .



    NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



    Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

    :

    long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






    C# IL , :

    long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





    , C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

    , :

    long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








    , List.ForEach , Action<>



    , . callvirt IL , , callvirt. , , List.ForEach .

    , , List :

    for (int i = 0; i < List.Count; i++)






    for (int i = 0; i < NUM_ITEMS; i++)





    foreach (int i in List)





    List.ForEach(delegate(int i) { result += i; });







    , :

    画像

    . , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

    , , " ":

    画像

    , , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



    , Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

    , , . 5 , 10000000 50000000 .



    :

    画像



    :

    画像



    : ForEach List, . Array.ForEach




    ( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



    .



    :

    .. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







    , , , 1000 . 1000 , . , , .

    :

    画像

    , . ForEach .
































































    System.Collections.Generic.IEnumerator System.Collections.IEnumerator



    . : " ?" , , , , .



    NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



    Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

    :

    long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






    C# IL , :

    long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





    , C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

    , :

    long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








    , List.ForEach , Action<>



    , . callvirt IL , , callvirt. , , List.ForEach .

    , , List :

    for (int i = 0; i < List.Count; i++)






    for (int i = 0; i < NUM_ITEMS; i++)





    foreach (int i in List)





    List.ForEach(delegate(int i) { result += i; });







    , :

    画像

    . , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

    , , " ":

    画像

    , , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



    , Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

    , , . 5 , 10000000 50000000 .



    :

    画像



    :

    画像



    : ForEach List, . Array.ForEach




    ( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



    .



    :

    .. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







    , , , 1000 . 1000 , . , , .

    :

    画像

    , . ForEach .
































































     System.Collections.Generic.IEnumerator   System.Collections.IEnumerator
          
          



    . : " ?" , , , , .



    NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



    Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

    :

    long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






    C# IL , :

    long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





    , C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

    , :

    long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








    , List.ForEach , Action<>



    , . callvirt IL , , callvirt. , , List.ForEach .

    , , List :

    for (int i = 0; i < List.Count; i++)






    for (int i = 0; i < NUM_ITEMS; i++)





    foreach (int i in List)





    List.ForEach(delegate(int i) { result += i; });







    , :

    画像

    . , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

    , , " ":

    画像

    , , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



    , Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

    , , . 5 , 10000000 50000000 .



    :

    画像



    :

    画像



    : ForEach List, . Array.ForEach




    ( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



    .



    :

    .. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







    , , , 1000 . 1000 , . , , .

    :

    画像

    , . ForEach .
































































    System.Collections.Generic.IEnumerator System.Collections.IEnumerator



    . : " ?" , , , , .



    NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



    Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

    :

    long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






    C# IL , :

    long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





    , C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

    , :

    long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








    , List.ForEach , Action<>



    , . callvirt IL , , callvirt. , , List.ForEach .

    , , List :

    for (int i = 0; i < List.Count; i++)






    for (int i = 0; i < NUM_ITEMS; i++)





    foreach (int i in List)





    List.ForEach(delegate(int i) { result += i; });







    , :

    画像

    . , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

    , , " ":

    画像

    , , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



    , Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

    , , . 5 , 10000000 50000000 .



    :

    画像



    :

    画像



    : ForEach List, . Array.ForEach




    ( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



    .



    :

    .. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







    , , , 1000 . 1000 , . , , .

    :

    画像

    , . ForEach .
































































    System.Collections.Generic.IEnumerator System.Collections.IEnumerator



    . : " ?" , , , , .



    NET Framework 2.0 ( . : , ) , , . , , Action, Converter<TInput,TOutput>



    Predicate, . , . , . , List.ForEach(Action). , ForEach , Foreach?

    :

    long Sum(List<int> intList) { long result = 0; foreach (int i in intList) result += i; return result; }






    C# IL , :

    long Sum(List<int> intList) { long result = 0; List<T>.Enumerator enumerator = intList.GetEnumerator(); try { while (enumerator.MoveNext()) { int i = enumerator.Current; result += i; } } finally { enumerator.Dispose(); } return result; }





    , C # : IEnumerator .MoveNext () IEnumerator .Current. List.Enumerator ( IEnumerator) IL callvirt, .

    , :

    long Sum(List<int> intList) { long result = 0; intList.ForEach(delegate(int i) { result += i; }); result result; } , : long Sum(List<int> intList) { long result = 0; intList.ForEach(i => result += i); return result; }








    , List.ForEach , Action<>



    , . callvirt IL , , callvirt. , , List.ForEach .

    , , List :

    for (int i = 0; i < List.Count; i++)






    for (int i = 0; i < NUM_ITEMS; i++)





    foreach (int i in List)





    List.ForEach(delegate(int i) { result += i; });







    , :

    画像

    . , , List.ForEach , ! , ( ) Foreach , . , , , List.ForEach .

    , , " ":

    画像

    , , , , 50% . ForEach- 20%. List.ForEach , , - Foreach- . List.ForEach , Foreach.



    , Dell Inspiron 9400 Core Duo T2400 2 . , : ForEachTest.zip (3,82 ).

    , , . 5 , 10000000 50000000 .



    :

    画像



    :

    画像



    : ForEach List, . Array.ForEach




    ( ) , foreach. , , IEnumerator foreach- . , IL, . , foreach , , Array.ForEach



    .



    :

    .. Unity3D C#, . using UnityEngine; using System.Collections; using System.Collections.Generic; public class testspeed : MonoBehaviour { public enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} public LoopType type; public int Count=2; public int iterationCount=1000; private List<int> lint = new List<int>(); private int[] alint; private long result; void Start () { for (int i = 0 ; i<Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} } void Update () { for (int j =0; j<iterationCount;j++) { result=0; switch (type) { case LoopType.for_loop: for (int i = 0; i < lint.Count; i++) {result += lint[i];} break; case LoopType.for_loop_no_count: for (int i = 0; i < Count; i++){result += lint[i];} break; case LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; case LoopType.ListintForEach: lint.ForEach(i => result += i); break; } } } }







    , , , 1000 . 1000 , . , , .

    :

    画像

    , . ForEach .



































































All Articles