{"id":210616,"date":"2014-01-28T18:59:02","date_gmt":"2014-01-28T14:59:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=210616"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=210616","title":{"rendered":"<span class=\"post_title\">\u0427\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435? foreach vs. List.ForEach vs for-loop<\/span>"},"content":{"rendered":"<div class=\"content html_format\">       \u0421\u0435\u0433\u043e\u0434\u043d\u044f (<i>\u043f\u0440\u0438\u043c. \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430:<\/i> \u0442.\u0435. 6 \u043b\u0435\u0442 \u043d\u0430\u0437\u0430\u0434) \u044f \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u043b \u0441\u043f\u0438\u0441\u043e\u043a List, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e foreach, \u0438 \u0447\u0443\u0432\u0441\u0442\u0432\u0443\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0441\u0430\u043c\u043e\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u043e, \u043e\u0441\u043e\u0437\u043d\u0430\u0432\u0430\u044f \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u0435\u0435, \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b\u043b\u043e \u0431\u044b, \u043f\u043e\u043f\u044b\u0442\u0430\u0439\u0441\u044f \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ArrayList. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0447\u0443\u0434\u0443 Generic \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 C# \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e \u0438\u0437\u0431\u0435\u0433\u0430\u0435\u0442 \u043c\u043d\u043e\u0433\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u0443\u043f\u0430\u043a\u043e\u0432\u043e\u0447\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 <code>System.Collections.Generic.IEnumerator&lt;int&gt;<\/code> \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0430\u0440\u044b\u0445 <code>System.Collections.IEnumerator<\/code>. \u0422\u043e\u0433\u0434\u0430 \u044f \u043f\u043e\u0434\u0443\u043c\u0430\u043b: \u00ab\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0438 \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431?\u00bb \u041f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u0440\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e, \u043d\u0435\u0442, \u044d\u0442\u043e \u043d\u0435 \u0441\u0430\u043c\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431.<br \/>  <a name=\"habracut\"><\/a><br \/>  NET Framework 2.0 (<i>\u043f\u0440\u0438\u043c. \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430:<\/i> \u0434\u0430, \u0441\u0442\u0430\u0442\u044c\u044f \u0441\u0442\u0430\u0440\u0430\u044f) \u043f\u043e\u043b\u043e\u043d \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0445 \u0432\u043a\u0443\u0441\u043d\u044f\u0448\u0435\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0435\u043b\u0430\u044e\u0442 \u0436\u0438\u0437\u043d\u044c \u043b\u0435\u0433\u0447\u0435, \u0430 \u043a\u043e\u0434 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u0435\u0435. \u0421\u0440\u0435\u0434\u0438 \u043c\u043e\u0438\u0445 \u043b\u044e\u0431\u0438\u043c\u044b\u0445, \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0438 \u0441\u043f\u0438\u0441\u043a\u043e\u0432, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a <code>Action&lt;T&gt;<\/code>, <code>Converter&lt;TInput,TOutput&gt;<\/code> \u0438 <code>Predicate&lt;T&gt;<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0435 \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u044b. <a href=\"http:\/\/diditwith.net\/ct.ashx?id=506c0888-8c5f-40e5-9d39-a09e2ebf3a55&amp;url=http%3a%2f%2fjasonf-blog.blogspot.com%2f2006%2f09%2fdelegates.html\">\u0421\u0443\u0434\u044f \u043f\u043e \u0432\u0441\u0435\u043c\u0443, \u044f \u043e\u0434\u0435\u0440\u0436\u0438\u043c \u0438\u043c\u0438<\/a>. \u042d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0438\u044f\u044e\u0442, \u043a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0441 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0438 \u043b\u044f\u043c\u0431\u0434\u0430 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438. \u0412 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438, \u044f \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043b\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>List&lt;T&gt;.ForEach(Action&lt;T&gt;)<\/code>. \u042f \u0437\u0430\u0445\u043e\u0442\u0435\u043b \u0443\u0437\u043d\u0430\u0442\u044c, ForEach \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438\u043b\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043a\u0430\u043a \u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 Foreach?<br \/>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434:  <\/p>\n<pre><code class=\"cs\">long Sum(List&lt;int&gt; intList) {   long result = 0;   foreach (int i in intList)     result += i;   return result; } <\/code><\/pre>\n<p>  \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 C# \u0431\u0443\u0434\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c IL \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435 \u043a\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0440\u0430\u043d\u0441\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cs\">long Sum(List&lt;int&gt; intList) {   long result = 0;   List&lt;T&gt;.Enumerator enumerator = intList.GetEnumerator();   try   {     while (enumerator.MoveNext())     {       int i = enumerator.Current;       result += i;     }   }   finally   {     enumerator.Dispose();   }   return result; } <\/code><\/pre>\n<p>  \u041f\u043e \u0441\u0443\u0442\u0438, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 C # \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0434\u0432\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 \u0437\u0430 \u043e\u0434\u043d\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e: <code>IEnumerator &lt;T&gt;.MoveNext ()<\/code> \u0438 <code>IEnumerator &lt;T&gt;.Current<\/code>. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b <code>List&lt;T&gt;.Enumerator<\/code> (\u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 IEnumerator) \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0435 IL \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u043e callvirt, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u043a\u0440\u043e\u0448\u0435\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u043d\u0430 \u043d\u0438\u0436\u043d\u0435\u043c \u0443\u0440\u043e\u0432\u043d\u0435. <br \/>  \u0414\u043b\u044f \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u044f, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434:  <\/p>\n<pre><code class=\"cs\">long Sum(List&lt;int&gt; intList) {   long result = 0;   intList.ForEach(delegate(int i) { result += i; });   result result; } \u0418\u043b\u0438 \u0442\u043e\u0442 \u0436\u0435 \u043a\u043e\u0434, \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0439 \u043b\u044f\u043c\u0431\u0434\u0430 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f: long Sum(List&lt;int&gt; intList) {   long result = 0;   intList.ForEach(i =&gt; result += i);   return result; } <\/code><\/pre>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>List&lt;T&gt;.ForEach<\/code> \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434 \u0437\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e, \u0433\u0434\u0435 <code>Action&lt;\u0422&gt;<\/code> \u043b\u044e\u0431\u043e\u0439 \u0434\u0435\u043b\u0435\u0433\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0435. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c callvirt IL \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e, \u043d\u043e \u0434\u0432\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0447\u0435\u043c \u043e\u0434\u0438\u043d callvirt. \u0418\u0442\u0430\u043a, \u044f \u043e\u0436\u0438\u0434\u0430\u044e, \u0447\u0442\u043e <code>List&lt;T&gt;.ForEach<\/code> \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0431\u0443\u0434\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435.<br \/>  \u0412\u043e\u043e\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0439 \u043c\u043e\u0438\u043c \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c, \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0437\u0430\u043c\u0435\u0440\u044f\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 <code>List&lt;int&gt;<\/code> \u0434\u043b\u044f \u0447\u0435\u0442\u044b\u0440\u0451\u0445 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430:  <\/p>\n<ol>\n<li><code>for (int i = 0; i &lt; List&lt;int&gt;.Count; i++)<\/code><\/li>\n<li><code>for (int i = 0; i &lt; NUM_ITEMS; i++)<\/code><\/li>\n<li><code>foreach (int i in List&lt;int&gt;)<\/code><\/li>\n<li><code>List&lt;int&gt;.ForEach(delegate(int i) { result += i; });<\/code><\/li>\n<\/ol>\n<p>  \u041f\u0435\u0440\u0432\u044b\u0435, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u044f \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043b \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430:<br \/>  <img decoding=\"async\" src=\"http:\/\/habr.habrastorage.org\/post_images\/066\/d61\/0d0\/066d610d04142a750a09502654c52d29.png\" alt=\"image\"\/><br \/>  \u042d\u0442\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043d\u0435\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u044b. \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, <code>List&lt;int&gt;.ForEach<\/code> \u0434\u0430\u0436\u0435 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438! \u041d\u0435 \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442, \u0447\u0442\u043e (\u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430) Foreach \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a \u0436\u0435 \u0431\u044b\u0441\u0442\u0440, \u043a\u0430\u043a \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0446\u0438\u043a\u043b\u044b. \u0418\u0442\u0430\u043a, \u0435\u0441\u043b\u0438 \u0432\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430, \u0442\u043e <code>List&lt;int&gt;.ForEach<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u043c \u0431\u044b\u0441\u0442\u0440\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c.<br \/>  \u0414\u0430\u043b\u0435\u0435, \u044f \u043f\u0440\u043e\u0433\u043d\u0430\u043b \u043c\u043e\u0438 \u0442\u0435\u0441\u0442\u044b \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u043d\u044b\u0435 \u043a \u00ab\u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u043c\u0438\u0440\u0443\u00bb:<br \/>  <img decoding=\"async\" src=\"http:\/\/habr.habrastorage.org\/post_images\/b3c\/51a\/1c9\/b3c51a1c924d8bc6919d45c89b240e23.png\" alt=\"image\"\/><br \/>  \u0413\u043b\u044f\u0434\u044f \u043d\u0430 \u044d\u0442\u0438 \u0446\u0438\u0444\u0440\u044b, \u044f \u043d\u0430\u0445\u043e\u0434\u0438\u043b\u0441\u044f \u043f\u043e\u0434 \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u0435\u043d \u043e\u0442 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0446\u0438\u043a\u043b\u044b, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u043e\u043b\u0435\u0435, \u0447\u0435\u043c 50% \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438. ForEach-\u043f\u0435\u0442\u043b\u044f\u043c \u0442\u0430\u043a\u0436\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0443\u0440\u0435\u0437\u0430\u0442\u044c \u043e\u043a\u043e\u043b\u043e 20%. <code>List&lt;T&gt;.ForEach<\/code> \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0442\u0430\u043a \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0435, \u0447\u0442\u043e \u043e\u043d \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043e\u0431\u0433\u043e\u043d\u044f\u0435\u0442 Foreach-\u043f\u0435\u0442\u043b\u0438 \u0441\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043e\u0442\u0440\u044b\u0432\u043e\u043c. <code>List&lt;T&gt;.ForEach<\/code> \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 Foreach.<\/p>\n<p>  \u0417\u0430\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u044d\u0442\u0438 \u0442\u0435\u0441\u0442\u044b \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u043d\u0430 \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435 Dell Inspiron 9400 \u0441 Core Duo T2400 \u0438 2 \u0413\u0431 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u0440\u043e\u0433\u043d\u0430\u0442\u044c \u044d\u0442\u0438 \u0442\u0435\u0441\u0442\u044b \u0443 \u0441\u0435\u0431\u044f, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0437\u0434\u0435\u0441\u044c: <a href=\"http:\/\/diditwith.net\/ct.ashx?id=506c0888-8c5f-40e5-9d39-a09e2ebf3a55&amp;url=http%3a%2f%2fdiditwith.net%2fcontent%2fbinary%2fForEachTest.zip\">ForEachTest.zip (3,82 \u041a\u0431)<\/a>.<br \/>  \u0418 \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u0434\u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 \u0441\u0442\u043e\u0438\u0442 \u0442\u044b\u0441\u044f\u0447\u0438 \u0441\u043b\u043e\u0432, \u044f \u0441\u043e\u0437\u0434\u0430\u043b \u043f\u0430\u0440\u0443 \u0433\u0440\u0430\u0444\u0438\u043a\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u0432 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0442\u0435\u0441\u0442\u043e\u0432. \u041d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0430\u0445 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e 5 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u0432, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0435 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c 10000000 \u0432 50000000 \u0446\u0435\u043b\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. <\/p>\n<p>  \u0411\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430:<br \/>  <img decoding=\"async\" src=\"http:\/\/habr.habrastorage.org\/post_images\/919\/4d7\/c1a\/9194d7c1a48e7f06a8eb0996abfd854f.png\" alt=\"image\"\/><\/p>\n<p>  \u0421 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430:<br \/>  <img decoding=\"async\" src=\"http:\/\/habr.habrastorage.org\/post_images\/7f7\/770\/5ce\/7f77705cea95aab954d43ca57b317cb4.png\" alt=\"image\"\/><\/p>\n<p>  \u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446: \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u043c\u0435\u0442\u043e\u0434 ForEach \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 <code>List&lt;T&gt;<\/code>, \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438. \u041c\u0435\u0442\u043e\u0434 <code>Array.ForEach<\/code> \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 (\u0438\u043b\u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u0432) \u0438 \u043e\u043d \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0447\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 foreach. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u0434, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0439 <code>IEnumerator&lt;T&gt;<\/code> \u0434\u043b\u044f foreach-\u043f\u0435\u0442\u0435\u043b\u044c \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u043a\u0442\u043e\u0440\u044b. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e, \u043e\u043d \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 IL, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0432\u0435\u043a\u0442\u043e\u0440\u0430\u043c\u0438. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f foreach \u043d\u0430 \u0432\u0435\u043a\u0442\u043e\u0440\u0430\u0445, \u043c\u0435\u0442\u043e\u0434\u044b \u0432 \u0438\u0442\u043e\u0433\u0435 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a <code>Array.ForEach<\/code> \u0435\u0449\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0434\u0438\u043d \u0432\u044b\u0437\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u0430.<\/p>\n<p>  <b><i>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430:<\/i><\/b><br \/>  \u0422.\u043a. \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u043d\u0430 Unity3D \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e C#, \u0442\u043e \u044f \u043d\u0435 \u0441\u043c\u043e\u0433 \u0443\u0441\u0442\u043e\u044f\u0442\u044c \u043e\u0442 \u0441\u043e\u0431\u043b\u0430\u0437\u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u0432\u044b\u0432\u043e\u0434\u043e\u0432 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0441\u043a\u0440\u0438\u043f\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">using UnityEngine; using System.Collections; using System.Collections.Generic;  public class testspeed : MonoBehaviour {  \tpublic enum LoopType {none, for_loop, for_loop_no_count, foreach_loop, ListintForEach} \tpublic LoopType type; \tpublic int Count=2; \tpublic int iterationCount=1000; \tprivate List&lt;int&gt; lint = new List&lt;int&gt;(); \tprivate int[] alint; \tprivate long result;  \tvoid Start () { \t\tfor (int i = 0 ; i&lt;Count; i++) {int r=UnityEngine.Random.Range(0,1000); lint.Add(r);} \t} \t \tvoid Update () { \t\tfor (int j =0; j&lt;iterationCount;j++) \t\t{ \t\t\tresult=0; \t\t\tswitch (type) \t\t\t{ \t\t\tcase LoopType.for_loop: for (int i = 0; i &lt; lint.Count; i++) {result += lint[i];} break; \t\t\tcase LoopType.for_loop_no_count: int c = lint.Count; for (int i = 0; i &lt; c; i++){result += lint[i];} break; \t\t\tcase LoopType.foreach_loop: foreach (int i in lint) {result += i;} break; \t\t\tcase LoopType.ListintForEach: lint.ForEach(i =&gt; result += i); break; \t\t\t} \t\t}   \t} } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u044f \u0437\u0430\u043c\u0435\u0442\u0438\u043b \u043e\u0434\u043d\u043e \u043b\u0443\u043a\u0430\u0432\u0441\u0442\u0432\u043e \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0445 \u0432\u044b\u0448\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0435\u0435\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0438\u0436\u043d\u0435\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u0435\u0439 \u0434\u043b\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432\u044b\u0431\u0440\u0430\u043d\u043e 1000 \u0447\u043b\u0435\u043d\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430. \u0415\u0441\u043b\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 1000 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0442\u043e \u0432\u0441\u0451 \u0432\u044b\u0448\u0435\u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0435 \u0432\u0435\u0440\u043d\u043e. \u041d\u043e \u0443 \u043c\u0435\u043d\u044f \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0442\u0430\u043a\u043e\u0435 \u0442\u0430\u043a\u043e\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0435 \u0447\u0430\u0441\u0442\u043e, \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c\u0438, \u043d\u0443 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0442\u043d\u044f\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.<br \/>  \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043f\u0440\u0438\u0432\u043e\u0436\u0443 \u0441\u0432\u043e\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443:<br \/>  <img decoding=\"async\" src=\"http:\/\/habr.habrastorage.org\/post_images\/34e\/25a\/42a\/34e25a42ae4613fd7afd08367b957b52.png\" alt=\"image\"\/><br \/>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u0434\u043b\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u0441 \u0432\u043d\u0435\u0448\u043d\u0438\u043c \u0441\u0447\u0451\u0442\u0447\u0438\u043a\u043e\u043c. \u041f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 ForEach \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442 \u0435\u043c\u0443 \u0432 \u0440\u0430\u0437\u044b.   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/210616\/\"> http:\/\/habrahabr.ru\/post\/210616\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">       \u0421\u0435\u0433\u043e\u0434\u043d\u044f (<i>\u043f\u0440\u0438\u043c. \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430:<\/i> \u0442.\u0435. 6 \u043b\u0435\u0442 \u043d\u0430\u0437\u0430\u0434) \u044f \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u043b \u0441\u043f\u0438\u0441\u043e\u043a List, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e foreach, \u0438 \u0447\u0443\u0432\u0441\u0442\u0432\u0443\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0441\u0430\u043c\u043e\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u043e, \u043e\u0441\u043e\u0437\u043d\u0430\u0432\u0430\u044f \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u0435\u0435, \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b\u043b\u043e \u0431\u044b, \u043f\u043e\u043f\u044b\u0442\u0430\u0439\u0441\u044f \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ArrayList. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0447\u0443\u0434\u0443 Generic \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 C# \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e \u0438\u0437\u0431\u0435\u0433\u0430\u0435\u0442 \u043c\u043d\u043e\u0433\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u0443\u043f\u0430\u043a\u043e\u0432\u043e\u0447\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 <code>System.Collections.Generic.IEnumerator&lt;int&gt;<\/code> \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0430\u0440\u044b\u0445 <code>System.Collections.IEnumerator<\/code>. \u0422\u043e\u0433\u0434\u0430 \u044f \u043f\u043e\u0434\u0443\u043c\u0430\u043b: \u00ab\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0438 \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431?\u00bb \u041f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u0440\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e, \u043d\u0435\u0442, \u044d\u0442\u043e \u043d\u0435 \u0441\u0430\u043c\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-210616","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/210616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=210616"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/210616\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}