{"id":400500,"date":"2024-06-29T15:18:28","date_gmt":"2024-06-29T15:18:28","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=400500"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T15:18:28","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=400500","title":{"rendered":"<span>\u0423\u0441\u043a\u043e\u0440\u044f\u0435\u043c \u0446\u0438\u043a\u043b foreach \u0434\u043e for<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442! \u0425\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c \u043e\u043f\u044b\u0442\u0435, \u043a\u0430\u043a \u044f \u043f\u0438\u0441\u0430\u043b \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u0442\u0438\u043f\u0430 <code>Range<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0431\u044b \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0434\u0435\u0448\u0435\u0432\u044b\u043c, \u043a\u0430\u043a \u0446\u0438\u043a\u043b <code>for<\/code>.<\/p>\n<h2>\u0427\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c?<\/h2>\n<p>\u0423 System.Range, \u043a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u043e\u0447\u0435\u043d\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cs\">var a = 1..2; \/\/ \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442 new Range(1, 2)<\/code><\/pre>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u0430\u043a \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442, \u044f \u0440\u0435\u0448\u0438\u043b \u0430\u0431\u0443\u0437\u0438\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u0446\u0438\u043a\u043b\u0430 <code>foreach<\/code>, \u043a\u0430\u043a \u0437\u0430\u043c\u0435\u043d\u0430 <code>for<\/code>:<\/p>\n<pre><code class=\"cs\">foreach (var i in 1..5)     Console.Write(i);<\/code><\/pre>\n<p>(\u0432\u044b\u0432\u043e\u0434\u0438\u0442 12345)<\/p>\n<p>\u0414\u043b\u044f foreach \u0420\u043e\u0437\u043b\u0438\u043d \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>GetEnumerator<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b \u0431\u044b \u0447\u0442\u043e-\u0442\u043e, \u0443 \u0447\u0435\u0433\u043e \u0435\u0441\u0442\u044c <code>bool MoveNext()<\/code> \u0438 T Current. \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a \u043c\u0435\u0442\u043e\u0434 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cs\">public static class Extensions { public ... GetEnumerator(this System.Range range) => ... }<\/code><\/pre>\n<h2>Baseline (\u043d\u0430 \u0447\u0442\u043e \u0440\u0430\u0432\u043d\u044f\u0435\u043c\u0441\u044f)<\/h2>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435-\u0442\u043e <code>for<\/code> \u044d\u0442\u043e \u043a\u0443\u0434\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041d\u043e \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e \u043c\u044b \u0432\u0438\u0434\u0438\u043c<\/p>\n<pre><code class=\"cs\">for (int i = 0; i &lt; n; i++)<\/code><\/pre>\n<p>\u0442\u043e \u044d\u0442\u043e \u0441\u0442\u043e\u0438\u0442 \u0441\u0432\u043e\u0435\u0433\u043e \u043a\u0435\u0439\u0441\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>for<\/code>. \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441 \u0446\u0438\u043a\u043b\u043e\u043c <code>for<\/code> \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0442\u043e\u0438\u0442 <code>i += inc<\/code>:<\/p>\n<pre><code class=\"cs\">for (int i = 0; i &lt; n; i += inc)<\/code><\/pre>\n<p>\u041f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043d \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d \u0432\u0435\u0440\u0441\u0438\u0438 \u0441 i++:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>i++<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.380 us<\/p>\n<\/td>\n<td>\n<p>0.0534 us<\/p>\n<\/td>\n<td>\n<p>0.0446 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>i += inc<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.385 us<\/p>\n<\/td>\n<td>\n<p>0.0575 us<\/p>\n<\/td>\n<td>\n<p>0.0685 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u041a\u043e\u0434 \u043c\u0435\u0442\u043e\u0434\u0430 \u0441 for-\u043e\u043c:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForWithCustomIncrement() {     var iters = Iterations;     var a = 0;     var inc = Inc;     for (int i = 0; i &lt; iters; i += inc)         a += i;     return a; }<\/code><\/pre>\n<p>(\u043c\u044b \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0438 \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u0435 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0440\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438)<\/p>\n<p><a href=\"https:\/\/sharplab.io\/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKBuIGYACMxgIRgDswALfbKANa4aAbxqMJjcZIaMAlhwyMAkhhhRsGORA65GIxgHMYGANyNcJ8wF8a0iQAcocgG6aY8xSq76jVi\/7WjAC8jOTmAPQRjMAAnoyw2AAmCoaMAGZQEPgZcjAANkkRThAO6hixaIwY3B4AUsoAKoyQ+A5y+eqMAO46AORKpVr4cgBeHnJKGBCeYJ64GFAArmBaOnbUklKbMkwKSgBi0ADqk9zKXLD4nBisseQAFACU9vqvW25Q8mpQeqGq6k02l0pnekk+jGwIUYAAZQTsthJ0tBGA99vJoXCMQAeb7qXDmOQAaiJLwRiK2UKJoTk8IpEmIAHZIXTJLZaOTXrJ0UcoKcagBhJYLbIXMBXG7PV5ickffh437QgEaNYgsESCFQ0Jw9WMCEKOb\/LisinIr5orxyTGExi4yb4m3U2Zk+mIqk0k2Ipks17s6xAA===\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u0434\u0433\u0435\u043d <\/a>for-\u0430:<\/p>\n<pre><code>    L000f: add edx, r8d     L0012: add r8d, ecx     L0015: cmp r8d, eax     L0018: jl short L000f<\/code><\/pre>\n<blockquote>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043a\u043e\u0434 \u043e\u0434\u043d\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0446\u0438\u043a\u043b\u0430, \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0432\u0435\u0440\u0445\u0435\u0434 \u0438 \u043e\u043a\u0440\u0443\u0436\u0430\u044e\u0449\u0438\u0439 \u043c\u0443\u0441\u043e\u0440 \u043d\u0430\u0441 \u043d\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442.<\/p>\n<\/blockquote>\n<h2>\u042d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 1. Enumerable.Range<\/h2>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0438\u0437 BCL: Enumerable.Range, \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0438\u0437\u0443\u0447\u0430\u0442\u044c <a href=\"https:\/\/source.dot.net\/#System.Linq\/System\/Linq\/Range.cs,11\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0422\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0430\u0448 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForeachWithEnumerableRange() {     var a = 0;     foreach (var i in Enumerable.Range(0, Iterations))         a += i;     return a; }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0420\u043e\u0437\u043b\u0438\u043d\u043e\u043c <a href=\"https:\/\/sharplab.io\/#v2:EYLgxg9gTgpgtADwGwBYA+ABATARgLABQGADAAQY4B0AMgJYB2AjgNyGEYDM5WpAQjPTAALALYBDKAGsAzoQDehUktKLlnUgwAupAJKaYUMZtoR600nNIBzGJualpt+wF82BZaQAOUWgDcjMBr02jqCFtZODpHOpAC8pDj2APRJpMAAnqSwYgAmDFakAGZQECJFtDAANjlJ3hCeBprpADSkmkKBAFI6ACqkkCKetJUGpADupgDk2vXGIrQAXoG02poQQWBB0ppQAK5gxqZuHqpK6lqkAGLQMGLCAOorQgCi9LsiBmLAIwBKYvQ2AAUAEpThYwR5\/FBSGI4qRiKx3B5lIUbnchKRAVCNEFSK93p9vjBKH8ATBAcRWnpPoczMDQUjkcjYQBqeK0RFMs4AdhhnOUrgIziAA\" rel=\"noopener noreferrer nofollow\">\u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f <\/a>\u043a\u0430\u043a<\/p>\n<pre><code class=\"cs\">public int ForeachWithEnumerableRange() {     int num = 0;     IEnumerator&lt;int> enumerator = Enumerable.Range(0, Iterations).GetEnumerator();     try     {         while (enumerator.MoveNext())         {             int current = enumerator.Current;             num += current;         }         return num;     }     finally     {         if (enumerator != null)         {             enumerator.Dispose();         }     } }<\/code><\/pre>\n<p>\u0418\u0437\u0443\u0447\u0430\u0442\u044c \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u043d\u044b\u0439 \u043a\u043e\u0434\u0433\u0435\u043d \u0441\u043c\u044b\u0441\u043b\u0430 \u043d\u0435\u0442, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0437\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>ForWithCustomIncrement<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.448 us<\/p>\n<\/td>\n<td>\n<p>0.0689 us<\/p>\n<\/td>\n<td>\n<p>0.0896 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithEnumerableRange<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>43.946 us<\/p>\n<\/td>\n<td>\n<p>0.8685 us<\/p>\n<\/td>\n<td>\n<p>1.2456 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>\u042d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 2. yield return<\/h2>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0435 \u0441\u043f\u043e\u0441\u043e\u0431 &#8212; \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434-\u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440, \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"cs\">private static IEnumerable&lt;int> MyRange(int from, int to, int inc) {     for (int i = from; i &lt;= to; i += inc)         yield return i; }  [Benchmark] public int ForeachWithYieldReturn() {     var a = 0;     foreach (var i in MyRange(0, Iterations - 1, 1))         a += i;     return a; }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043d\u0446\u0438\u043f\u0438\u0430\u043b\u044c\u043d\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u044b \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c, \u043d\u0430\u0448 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u044d\u0442\u043e \u0432\u0441\u0435 \u0435\u0449\u0435 \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0439 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441 \u0442\u0438\u043f \u0441 \u043a\u0443\u0447\u0435\u0439 \u043f\u043e\u043b\u0435\u0439. \u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0430\u044e\u0442, \u0447\u0442\u043e \u0441 yield-\u043e\u043c \u0434\u0430\u0436\u0435 \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u0441 Enumerable.Range:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>ForWithCustomIncrement<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.548 us<\/p>\n<\/td>\n<td>\n<p>0.0661 us<\/p>\n<\/td>\n<td>\n<p>0.1320 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithEnumerableRange<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>51.663 us<\/p>\n<\/td>\n<td>\n<p>0.9103 us<\/p>\n<\/td>\n<td>\n<p>1.2460 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithYieldReturn<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>56.580 us<\/p>\n<\/td>\n<td>\n<p>0.3561 us<\/p>\n<\/td>\n<td>\n<p>0.2780 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>\u042d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 3. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440. \u0427\u0442\u043e\u0431\u044b \u043e\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e \u0441 foreach-\u0435\u043c, \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b \u043c\u0435\u0442\u043e\u0434 <code>bool MoveNext()<\/code> \u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>T Current<\/code> (\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <code>int Current<\/code>).<\/p>\n<pre><code class=\"cs\">public struct RangeEnumerator {     private readonly int to;     private readonly int step;      private int curr;      public RangeEnumerator(int @from, int to, int step)     {         this.to = to + step;         this.step = step;         this.curr = @from - step;     }      public bool MoveNext()     {         curr += step;         return curr != to;     }          public int Current => curr; }<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0420\u043e\u0437\u043b\u0438\u043d \u0443\u0432\u0438\u0434\u0435\u043b, \u0447\u0442\u043e \u043f\u043e <code>System.Range<\/code> \u043c\u043e\u0436\u043d\u043e \u0444\u043e\u0440\u044b\u0447\u0438\u0442\u044c\u0441\u044f, \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cs\">public static class Extensions {     public static RangeEnumerator GetEnumerator(this Range @this)         => (@this.Start, @this.End) switch         {             ({ IsFromEnd: true, Value: 0 }, { IsFromEnd: true, Value: 0 }) => new RangeEnumerator(0, int.MaxValue, 1),             ({ IsFromEnd: true, Value: 0 }, { IsFromEnd: false, Value: var to }) => new RangeEnumerator(0, to + 1, 1),             ({ IsFromEnd: false, Value: var from }, { IsFromEnd: true, Value: 0 }) => new RangeEnumerator(from, int.MaxValue, 1),             ({ IsFromEnd: false, Value: var from }, { IsFromEnd: false, Value: var to })                 => (from &lt; to) switch                 {                     true => new RangeEnumerator(from, to, 1),                     false => new RangeEnumerator(from, to, -1),                 },             _ => throw new InvalidOperationException(\"Invalid range\")         }; }<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForeachWithRangeEnumerator() {     var a = 0;     foreach (var i in 0..(Iterations - 1))         a += i;     return a; }<\/code><\/pre>\n<p>\u0418 \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0420\u043e\u0437\u043b\u0438\u043d\u043e\u043c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">public int ForeachWithRangeEnumerator() {     int num = 0;     RangeEnumerator enumerator = Extensions.GetEnumerator(new Range(0, Iterations - 1));     while (enumerator.MoveNext())     {         int current = enumerator.Current;         num += current;     }     return num; }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u043d\u0435\u0442 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441 \u0442\u0438\u043f\u043e\u0432, \u0430 \u0442\u0430\u043a \u043a\u0430\u043a <code>RangeEnumerator<\/code> \u043d\u0435 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0442 <code>IDisposable<\/code>, \u0442\u043e \u0438 \u043d\u0435\u0442 try-catch \u0431\u043b\u043e\u043a\u043e\u0432 \u0432\u043e\u043a\u0440\u0443\u0433. \u0412\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>ForWithCustomIncrement<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.477 us<\/p>\n<\/td>\n<td>\n<p>0.0690 us<\/p>\n<\/td>\n<td>\n<p>0.0989 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithEnumerableRange<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>50.501 us<\/p>\n<\/td>\n<td>\n<p>0.9984 us<\/p>\n<\/td>\n<td>\n<p>1.2627 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithYieldReturn<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>53.782 us<\/p>\n<\/td>\n<td>\n<p>1.0583 us<\/p>\n<\/td>\n<td>\n<p>0.9900 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithRangeEnumerator<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>18.061 us<\/p>\n<\/td>\n<td>\n<p>0.1731 us<\/p>\n<\/td>\n<td>\n<p>0.1619 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>18 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u043a\u0443\u043d\u0434 \u043d\u0430 10000 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u041d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0443\u0447\u0448\u0435, \u0447\u0435\u043c \u0430\u043d\u0430\u043b\u043e\u0433\u0438 \u0434\u043e \u044d\u0442\u043e\u0433\u043e, \u043d\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0445\u0443\u0436\u0435, \u0447\u0435\u043c <code>for<\/code>. \u0412\u0440\u0435\u043c\u044f \u0432\u0437\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 \u043a\u043e\u0434\u0433\u0435\u043d!<\/p>\n<p>\u041e\u0434\u043d\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0446\u0438\u043a\u043b\u0430 <a href=\"https:\/\/sharplab.io\/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKBuIGYACMxgIRgDswALfbKANa4aAbxqMJzJgEsOGRgEkMMKNgzSIHXIxGMA5jAwBuRrkMmAvjXGSADlGkA3NTEaz5Crjv3nTvi4wAvIzkJgD0YYzAAJ6MsNgAJrJ6jABmUBD4adIwADYJYfYQtioY0WiMGNyuAFIKACqMkPi20rkqjADumgDk8sXq+NIAXq7S8hgQbl6yuBhQAK5g6prW1JKMaxsM0\/IAYtAw2DwA6uPcAErYHAYAohwL+Cpq0AAUAJQ2EmLrG5LOUEY2CCjAADEYvn9UodjtxGK8AW5pmCAHQo15KZ4rLSMOAhd6fX5\/P7AgDUwWkEKJf2IAHYgVSNlZqMydsRyEhmKRGLcEMotBotJCfsSJGyOYwrjcYPdHljoIwAOKGWVPVSTKCvKrSbRSgzMYja3CE0V\/QIAPnhxEN3B1KIAyhh+BgKtajSj7gl3qZOuMeJDTSLTaLXroFLg9hl8J6QJVFjAKgA1bC5BYwWOgxgWCphiNRmNxtNJlNpjNZ70WxgcGCdSXXO4PNUvTWgiruFEAWWwCGTqYT+LQAeDElDijzmQL8yLjF7pbBWZzY8jE44CVjqRTZmLfdjiMm5aClurtb1Msb8pbFX3pJCFXI70H1OHI9zy+jq\/Xm\/7s\/TjER6UyBdvHDN9J3jbc50zCwKyPGs62lVUL1eAD8DbORO27H87wfIdh1HED8w\/NIvwg39\/yjIDX0ItdiNyLcZxLMj+EqKZoNw58JErZCKIAHhY71cF9DB\/SfDjJCDMTgynVxK2PeCGzldU3hQq8IGwx9JOHDc6Jk2CT3rM9FObbjMlUio4HvDTNKZKzJIAfUPSpuAyWs5M8ZxcmkBIAHkSnVQVeTAGBbGxV4ACJ3JTLy4gMsKTWfCxGUkZktkkHY5kWZZ5MMpsNWFdj7CcFw4iOBJNFyWJ3BYpLiUK5xlBKxJysquRTGUWwqQKhx6rGVqwAWKAoE60SJHcFQOBTZgUEYAARGBIC0KdlleCAFnkKrrRUxhVvW1rJgqHbdja4L4uHLjNqjMzjtsCt4X6war1tXAUX2py7TmE7hvYsaoAm3JssQpTNQ24gtqq16qo+m72IkqSnpeqZgmva6arh972pBKHUdNd17sBYILsAvEsfYlLaBGqQoggCB\/o7CBHBgAA5GA+Q+GH2I2PHGHJFGObS+kuYAQiRiBseS1LiR2KqAGEBtgVrKzx1GLCAA==\" rel=\"noopener noreferrer nofollow\">\u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 <\/a>\u0442\u0430\u043a:<\/p>\n<pre><code>    L003e: add esi, eax     L0040: mov eax, [rsp+0x38]     L0044: add eax, [rsp+0x34]     L0048: mov [rsp+0x38], eax     L004c: mov eax, [rsp+0x38]     L0050: cmp eax, [rsp+0x30]     L0054: jne short L003a<\/code><\/pre>\n<p>\u0414\u0430, \u0443 \u043d\u0430\u0441 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441-\u0442\u0438\u043f\u043e\u0432 \u043d\u0435\u0442, \u043d\u043e \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0432 \u043f\u0430\u043c\u044f\u0442\u0438! \u0425\u043e\u0442\u044c \u0438 \u043d\u0430 \u0441\u0442\u0435\u043a\u0435. <\/p>\n<h2>\u042d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 4. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u044f\u0447\u0435\u043c \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/h2>\n<p>\u0427\u0442\u043e \u0437\u0430 \u043c\u0430\u0433\u0438\u044f? \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0448 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u043c, \u043a\u0430\u043a <code>for<\/code>, \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u043f\u043e\u043b\u044f \u0432 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b. \u041d\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u0430\u043c \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u043b, \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 \u0431\u044b\u043b\u043e, \u0438 \u043e\u043d \u043d\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u043b \u043d\u0443\u0436\u043d\u044b\u0435. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u043b\u0435\u0433\u0447\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<p>\u0418\u0437 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0430 3:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForeachWithRangeEnumeratorRaw() {     var a = 0;     var enumerator = (0..(Iterations - 1)).GetEnumerator();     while (enumerator.MoveNext())         a += enumerator.Current;     return a; }<\/code><\/pre>\n<p>(\u044f \u0437\u0430\u043c\u0435\u043d\u0438\u043b <code>foreach<\/code> \u043d\u0430 \u0442\u043e\u0447\u043d\u043e \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0441 \u043f\u043e\u0441\u0442\u0444\u0438\u043a\u0441\u043e\u043c <code>Raw<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0435 \u0440\u0443\u0447\u043a\u0430\u043c\u0438, \u0434\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438. \u0421\u043c. \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 3, \u0442\u0430\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e, \u043a\u0430\u043a \u0420\u043e\u0437\u043b\u0438\u043d \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 <code>foreach<\/code>).<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u0441\u043f\u0440\u044f\u0447\u0435\u043c \u0446\u0438\u043a\u043b \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForeachWithRangeEnumeratorRawWithLocal() {     var enumerator = (0..(Iterations - 1)).GetEnumerator();             return EnumerateItAll(enumerator);      static int EnumerateItAll(RangeEnumerator enumerator)     {         var a = 0;         while (enumerator.MoveNext())             a += enumerator.Current;         return a;     } }<\/code><\/pre>\n<p>\u0418 \u043f\u043e\u0431\u0435\u043d\u0447\u0438\u043c:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>ForWithCustomIncrement<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.498 us<\/p>\n<\/td>\n<td>\n<p>0.0658 us<\/p>\n<\/td>\n<td>\n<p>0.1153 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithEnumerableRange<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>43.313 us<\/p>\n<\/td>\n<td>\n<p>0.8207 us<\/p>\n<\/td>\n<td>\n<p>1.0079 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithYieldReturn<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>56.963 us<\/p>\n<\/td>\n<td>\n<p>1.0638 us<\/p>\n<\/td>\n<td>\n<p>1.0448 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithRangeEnumerator<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>17.931 us<\/p>\n<\/td>\n<td>\n<p>0.2047 us<\/p>\n<\/td>\n<td>\n<p>0.1915 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithRangeEnumeratorRaw<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>17.932 us<\/p>\n<\/td>\n<td>\n<p>0.1486 us<\/p>\n<\/td>\n<td>\n<p>0.1390 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithRangeEnumeratorRawWithLocal<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.501 us<\/p>\n<\/td>\n<td>\n<p>0.0678 us<\/p>\n<\/td>\n<td>\n<p>0.0807 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><code>ForeachWithRangeEnumeratorRawWithLocal<\/code> &#8212; \u044d\u0442\u043e \u043a\u0430\u043a \u0440\u0430\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439. \u0418&#8230; \u043e\u043d \u043f\u043e\u0447\u0442\u0438 \u0432 \u0448\u0435\u0441\u0442\u044c \u0440\u0430\u0437 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u0437\u0430\u0438\u043d\u043b\u0430\u0439\u043d\u0435\u043d\u043d\u044b\u0439! \u041a\u0430\u043a \u0436\u0435 \u0442\u0430\u043a \u0432\u044b\u0448\u043b\u043e-\u0442\u043e?<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u043c <a href=\"https:\/\/sharplab.io\/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKBuIGYACMxgIRgDswALfbKANa4aAbxqMJzJgEsOGRgEkMMKNgzSIHXIxGMA5jAwBuRrkMmAvjXGSADlGkA3NTEaz5Crjv3nTvi4wAvIzkJgD0YYzAAJ6MsNgAJrJ6jABmUBD4adIwADYJYfYQtioY0WiMGNyuAFIKACqMkPi20rkqjADumgDk8sXq+NIAXq7S8hgQbl6yuBhQAK5g6prW1JKMaxsM0\/IAYtAw2DwA6uPcAErYHAYAohwL+Cpq0FedZ1UAMhBg2LkAFABKGwSMTrDaSZxQRicR7PSbQ4L\/AAMADpUf8lPCNFpGHAQoDAaiAOKGe5w1QIoEmCESEEQ4gAdkY5KelJgSgAgrkAbC2S8oICjFtacxyEhdiyHvzlFyef8rjcYKz4dAYdLVYL6RswaKIVDGNggoxkcLwXqJJ1uG1XP8+ZrUQBZCCOGAAORgCAwQOB5otGyNAGpgvbKdBUQBhBZQWByM3+iRMw3x0VWP1pjb0mhpnbEcXMUgsr2cXA44R+3Wi3P5xV3DVh6GkjAqhv\/KrSbS11zEYjt3C+hNBAB8jH+Pb7qIAyhh+BgKuPrbhUfcEoDTJ1xjxtaLK4OJP9dApcHsMvgVyBKosYBUAGp\/BYwC\/IxgWCqH4+n8+Xh+3++Pk0vmugQjhwMCdIwXYtgKKIVO4TrYAgd65D+BJoNu\/oHooH6ZF+8woUhD5Pi+b5YSeOEcAkF6pH8Zi\/sh\/4GpMgHDowoHgZB9bQciFRMYGIQVOQgJoX6g6YUeZFnhRVE0dejAEQx\/BpKexHeOJn5Sd+snyURFhASBYEQdcdYUtB6SZLBcjwYhf4CUJ6EWmJ2GSZRaQyXRhGMAaZlZK+qlOV+1G5LRcl\/hejFTLp9mDsBo7eYwAA8lQQGuuAbhgW4iXuoJRVlmksWxhlKlBVLeTxEC2cJuUWoFZj5QZHEmSVp5lRUcCCZVVWSK+OW0gA+ixVQZOBBWeM4uTSAkADyJSUjitwIGAMC2CsHD\/AARKNfwTXERkwGtA4JhYKYSGmIqJkwcyLMshXGTK0D0ruEL2E4LhxEcCSaLksTuElx1PQ4zjKG9iSfd9cimMothmvZz2A2M4NgNGUDQ5lEjuCoHB\/MwKCMAAIjAkBaHhyz\/BACzyD9PalYwZMU+DkwVLTkpzEtB3+jFY7ENTDMQ6zxr\/IjMY8YuqI8xOLO2EKaz2ejUCY7kN3KpxVKU1zzWSjzP0S2zEKPXqE5McEvG81DPWVCLEvGhLf36yLguIswauZHiJs2110uo1IUQQBACvOq6HpekC9l66K9uMMGrtm0m4cAIRGxAbsnWdDIyODUYxpw8gxfbf0WEAA=\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434\u0433\u0435\u043d<\/a>:<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0437\u0430\u0438\u043d\u043b\u0430\u0439\u043d\u0438\u043b\u0430\u0441\u044c:<\/p>\n<pre><code>Benchmarks.ForeachWithRangeEnumeratorRawWithLocal()     ...     L0044: call Benchmarks.&lt;ForeachWithRangeEnumeratorRawWithLocal>g__EnumerateItAll|8_0(RangeEnumerator)<\/code><\/pre>\n<p>\u0418 \u044d\u0442\u043e \u043d\u0430\u043c \u043d\u0430 \u0440\u0443\u043a\u0443. \u0421\u043c\u043e\u0442\u0440\u0438\u043c \u044d\u0442\u0443 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code>    L0000: mov eax, [rcx]     L0002: mov edx, [rcx+4]     L0005: mov ecx, [rcx+8]     L0008: xor r8d, r8d     L000b: jmp short L0010     L000d: add r8d, ecx         |     L0010: add ecx, edx         |     L0012: cmp ecx, eax         |     L0014: jne short L000d      |     L0016: mov eax, r8d     L0019: ret<\/code><\/pre>\n<p>\u041e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0447\u0435\u0442\u044b\u0440\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0438,<\/p>\n<pre><code>    L000d: add r8d, ecx     L0010: add ecx, edx     L0012: cmp ecx, eax     L0014: jne short L000d<\/code><\/pre>\n<p>\u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u043d\u0430\u0448\u0430 \u043f\u043e\u0431\u0435\u0434\u0430, \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 <code>for<\/code>-\u0430.<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u0442\u0440\u0438 \u0441\u0442\u0440\u043e\u0447\u043a\u0438<\/p>\n<pre><code>    L0000: mov eax, [rcx]     L0002: mov edx, [rcx+4]     L0005: mov ecx, [rcx+8]<\/code><\/pre>\n<p>\u0412\u044b\u0433\u0440\u0443\u0436\u0430\u044e\u0442 \u0432\u0441\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e, \u0432 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b, \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434.<\/p>\n<h2>\u0412\u044b\u0432\u043e\u0434<\/h2>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0445\u043e\u0442\u044c \u0438 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c, \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u043f\u0440\u043e\u0449\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>for<\/code>, \u0447\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0441\u044f \u043b\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u0430\u0448 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u0432 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b.<\/p>\n<p>\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e \u0438\u043d\u043b\u0430\u0439\u043d\u0438\u043d\u0433 \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043b\u0443\u0447\u0448\u0435, \u043d\u043e \u0438 \u0441\u0438\u043b\u044c\u043d\u043e \u0445\u0443\u0436\u0435 (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 3.5 \u0434\u043e 18 us \u043d\u0430 10000 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439).<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<p><strong>UPD<\/strong>: \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u043d\u043e\u0435 \u043d\u0430 \u0432\u0438\u043d\u0434\u0435 \u0438 \u043b\u0438\u043d\u0443\u043a\u0441\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u044f WithLocal \u043c\u0435\u0442\u043e\u0434-\u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a, \u0443 <a class=\"mention\" href=\"\/users\/distortneo\">@DistortNeo<\/a> \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0441\u043e\u0432\u0441\u0435\u043c \u0438\u043d\u043e\u0439 \u043e\u0442 \u0432\u0438\u043d\u0434\u044b \u043a\u043e\u0434\u0433\u0435\u043d. \u0412\u043e\u0442 <a href=\"https:\/\/gist.github.com\/WhiteBlackGoose\/5c567281c0b6bbac816ae0d48607ccba\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a> \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0433\u0438\u0441\u0442 \u0441 \u043a\u043e\u0434\u0433\u0435\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430 WithLocal \u0434\u043b\u044f \u0432\u0438\u043d\u0434\u044b \u0438 \u043b\u0438\u043d\u0443\u043a\u0441\u0430. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e, \u0434\u0436\u0438\u0442 \u0437\u0430\u043c\u0435\u043d\u0438\u043b call \u043d\u0430 jmp, \u0430 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u043b \u043d\u0430 \u0441\u0442\u0435\u043a\u0435.<\/p>\n<h2>\u0421\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p>\u0412\u0435\u0441\u044c \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f \u043f\u0438\u0441\u0430\u043b, \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d \u0432 <a href=\"https:\/\/github.com\/WhiteBlackGoose\/FastForeachEnumeratorArticle\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 github<\/a>.<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/github.com\/WhiteBlackGoose\/FastForeachEnumeratorArticle\/blob\/main\/FastEnum\/Benchmarks\/Program.cs\" rel=\"noopener noreferrer nofollow\">\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/WhiteBlackGoose\/FastForeachEnumeratorArticle\/blob\/main\/FastEnum\/Library\/RangeEnumerator.cs\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u0434 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/WhiteBlackGoose\/FastForeachEnumeratorArticle\/blob\/main\/FastEnum\/Tests\/UnitTest1.cs\" rel=\"noopener noreferrer nofollow\">\u0422\u0435\u0441\u0442\u044b<\/a>, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u043d\u0438\u0435 \u043d\u0430 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/dotnet\/BenchmarkDotNet\" rel=\"noopener noreferrer nofollow\">BenchmarkDotNet<\/a> &#8212; \u0443\u0442\u0438\u043b\u0438\u0442\u0430\/\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0431\u0435\u043d\u0447\u0438\u043d\u0433\u0430<\/p>\n<\/li>\n<\/ul>\n<p>\u0411\u0435\u043d\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0430 \u0432\u0438\u043d\u0434\u0435<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/575664\/\"> https:\/\/habr.com\/ru\/articles\/575664\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442! \u0425\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c \u043e\u043f\u044b\u0442\u0435, \u043a\u0430\u043a \u044f \u043f\u0438\u0441\u0430\u043b \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u0442\u0438\u043f\u0430 <code>Range<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0431\u044b \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0434\u0435\u0448\u0435\u0432\u044b\u043c, \u043a\u0430\u043a \u0446\u0438\u043a\u043b <code>for<\/code>.<\/p>\n<h2>\u0427\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c?<\/h2>\n<p>\u0423 System.Range, \u043a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u043e\u0447\u0435\u043d\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cs\">var a = 1..2; \/\/ \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442 new Range(1, 2)<\/code><\/pre>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u0430\u043a \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442, \u044f \u0440\u0435\u0448\u0438\u043b \u0430\u0431\u0443\u0437\u0438\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u0446\u0438\u043a\u043b\u0430 <code>foreach<\/code>, \u043a\u0430\u043a \u0437\u0430\u043c\u0435\u043d\u0430 <code>for<\/code>:<\/p>\n<pre><code class=\"cs\">foreach (var i in 1..5)     Console.Write(i);<\/code><\/pre>\n<p>(\u0432\u044b\u0432\u043e\u0434\u0438\u0442 12345)<\/p>\n<p>\u0414\u043b\u044f foreach \u0420\u043e\u0437\u043b\u0438\u043d \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>GetEnumerator<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b \u0431\u044b \u0447\u0442\u043e-\u0442\u043e, \u0443 \u0447\u0435\u0433\u043e \u0435\u0441\u0442\u044c <code>bool MoveNext()<\/code> \u0438 T Current. \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a \u043c\u0435\u0442\u043e\u0434 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cs\">public static class Extensions { public ... GetEnumerator(this System.Range range) => ... }<\/code><\/pre>\n<h2>Baseline (\u043d\u0430 \u0447\u0442\u043e \u0440\u0430\u0432\u043d\u044f\u0435\u043c\u0441\u044f)<\/h2>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435-\u0442\u043e <code>for<\/code> \u044d\u0442\u043e \u043a\u0443\u0434\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041d\u043e \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e \u043c\u044b \u0432\u0438\u0434\u0438\u043c<\/p>\n<pre><code class=\"cs\">for (int i = 0; i &lt; n; i++)<\/code><\/pre>\n<p>\u0442\u043e \u044d\u0442\u043e \u0441\u0442\u043e\u0438\u0442 \u0441\u0432\u043e\u0435\u0433\u043e \u043a\u0435\u0439\u0441\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>for<\/code>. \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441 \u0446\u0438\u043a\u043b\u043e\u043c <code>for<\/code> \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0442\u043e\u0438\u0442 <code>i += inc<\/code>:<\/p>\n<pre><code class=\"cs\">for (int i = 0; i &lt; n; i += inc)<\/code><\/pre>\n<p>\u041f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043d \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d \u0432\u0435\u0440\u0441\u0438\u0438 \u0441 i++:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>i++<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.380 us<\/p>\n<\/td>\n<td>\n<p>0.0534 us<\/p>\n<\/td>\n<td>\n<p>0.0446 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>i += inc<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.385 us<\/p>\n<\/td>\n<td>\n<p>0.0575 us<\/p>\n<\/td>\n<td>\n<p>0.0685 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u041a\u043e\u0434 \u043c\u0435\u0442\u043e\u0434\u0430 \u0441 for-\u043e\u043c:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForWithCustomIncrement() {     var iters = Iterations;     var a = 0;     var inc = Inc;     for (int i = 0; i &lt; iters; i += inc)         a += i;     return a; }<\/code><\/pre>\n<p>(\u043c\u044b \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0438 \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u0435 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0440\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438)<\/p>\n<p><a href=\"https:\/\/sharplab.io\/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKBuIGYACMxgIRgDswALfbKANa4aAbxqMJjcZIaMAlhwyMAkhhhRsGORA65GIxgHMYGANyNcJ8wF8a0iQAcocgG6aY8xSq76jVi\/7WjAC8jOTmAPQRjMAAnoyw2AAmCoaMAGZQEPgZcjAANkkRThAO6hixaIwY3B4AUsoAKoyQ+A5y+eqMAO46AORKpVr4cgBeHnJKGBCeYJ64GFAArmBaOnbUklKbMkwKSgBi0ADqk9zKXLD4nBisseQAFACU9vqvW25Q8mpQeqGq6k02l0pnekk+jGwIUYAAZQTsthJ0tBGA99vJoXCMQAeb7qXDmOQAaiJLwRiK2UKJoTk8IpEmIAHZIXTJLZaOTXrJ0UcoKcagBhJYLbIXMBXG7PV5ickffh437QgEaNYgsESCFQ0Jw9WMCEKOb\/LisinIr5orxyTGExi4yb4m3U2Zk+mIqk0k2Ipks17s6xAA===\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u0434\u0433\u0435\u043d <\/a>for-\u0430:<\/p>\n<pre><code>    L000f: add edx, r8d     L0012: add r8d, ecx     L0015: cmp r8d, eax     L0018: jl short L000f<\/code><\/pre>\n<blockquote>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043a\u043e\u0434 \u043e\u0434\u043d\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0446\u0438\u043a\u043b\u0430, \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0432\u0435\u0440\u0445\u0435\u0434 \u0438 \u043e\u043a\u0440\u0443\u0436\u0430\u044e\u0449\u0438\u0439 \u043c\u0443\u0441\u043e\u0440 \u043d\u0430\u0441 \u043d\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442.<\/p>\n<\/blockquote>\n<h2>\u042d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 1. Enumerable.Range<\/h2>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0438\u0437 BCL: Enumerable.Range, \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0438\u0437\u0443\u0447\u0430\u0442\u044c <a href=\"https:\/\/source.dot.net\/#System.Linq\/System\/Linq\/Range.cs,11\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0422\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0430\u0448 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForeachWithEnumerableRange() {     var a = 0;     foreach (var i in Enumerable.Range(0, Iterations))         a += i;     return a; }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0420\u043e\u0437\u043b\u0438\u043d\u043e\u043c <a href=\"https:\/\/sharplab.io\/#v2:EYLgxg9gTgpgtADwGwBYA+ABATARgLABQGADAAQY4B0AMgJYB2AjgNyGEYDM5WpAQjPTAALALYBDKAGsAzoQDehUktKLlnUgwAupAJKaYUMZtoR600nNIBzGJualpt+wF82BZaQAOUWgDcjMBr02jqCFtZODpHOpAC8pDj2APRJpMAAnqSwYgAmDFakAGZQECJFtDAANjlJ3hCeBprpADSkmkKBAFI6ACqkkCKetJUGpADupgDk2vXGIrQAXoG02poQQWBB0ppQAK5gxqZuHqpK6lqkAGLQMGLCAOorQgCi9LsiBmLAIwBKYvQ2AAUAEpThYwR5\/FBSGI4qRiKx3B5lIUbnchKRAVCNEFSK93p9vjBKH8ATBAcRWnpPoczMDQUjkcjYQBqeK0RFMs4AdhhnOUrgIziAA\" rel=\"noopener noreferrer nofollow\">\u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f <\/a>\u043a\u0430\u043a<\/p>\n<pre><code class=\"cs\">public int ForeachWithEnumerableRange() {     int num = 0;     IEnumerator&lt;int> enumerator = Enumerable.Range(0, Iterations).GetEnumerator();     try     {         while (enumerator.MoveNext())         {             int current = enumerator.Current;             num += current;         }         return num;     }     finally     {         if (enumerator != null)         {             enumerator.Dispose();         }     } }<\/code><\/pre>\n<p>\u0418\u0437\u0443\u0447\u0430\u0442\u044c \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u043d\u044b\u0439 \u043a\u043e\u0434\u0433\u0435\u043d \u0441\u043c\u044b\u0441\u043b\u0430 \u043d\u0435\u0442, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0437\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>ForWithCustomIncrement<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.448 us<\/p>\n<\/td>\n<td>\n<p>0.0689 us<\/p>\n<\/td>\n<td>\n<p>0.0896 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithEnumerableRange<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>43.946 us<\/p>\n<\/td>\n<td>\n<p>0.8685 us<\/p>\n<\/td>\n<td>\n<p>1.2456 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>\u042d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 2. yield return<\/h2>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0435 \u0441\u043f\u043e\u0441\u043e\u0431 &#8212; \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434-\u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440, \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"cs\">private static IEnumerable&lt;int> MyRange(int from, int to, int inc) {     for (int i = from; i &lt;= to; i += inc)         yield return i; }  [Benchmark] public int ForeachWithYieldReturn() {     var a = 0;     foreach (var i in MyRange(0, Iterations - 1, 1))         a += i;     return a; }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043d\u0446\u0438\u043f\u0438\u0430\u043b\u044c\u043d\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u044b \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c, \u043d\u0430\u0448 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u044d\u0442\u043e \u0432\u0441\u0435 \u0435\u0449\u0435 \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0439 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441 \u0442\u0438\u043f \u0441 \u043a\u0443\u0447\u0435\u0439 \u043f\u043e\u043b\u0435\u0439. \u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0430\u044e\u0442, \u0447\u0442\u043e \u0441 yield-\u043e\u043c \u0434\u0430\u0436\u0435 \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u0441 Enumerable.Range:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>ForWithCustomIncrement<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.548 us<\/p>\n<\/td>\n<td>\n<p>0.0661 us<\/p>\n<\/td>\n<td>\n<p>0.1320 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithEnumerableRange<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>51.663 us<\/p>\n<\/td>\n<td>\n<p>0.9103 us<\/p>\n<\/td>\n<td>\n<p>1.2460 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithYieldReturn<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>56.580 us<\/p>\n<\/td>\n<td>\n<p>0.3561 us<\/p>\n<\/td>\n<td>\n<p>0.2780 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>\u042d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 3. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440. \u0427\u0442\u043e\u0431\u044b \u043e\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e \u0441 foreach-\u0435\u043c, \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b \u043c\u0435\u0442\u043e\u0434 <code>bool MoveNext()<\/code> \u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>T Current<\/code> (\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <code>int Current<\/code>).<\/p>\n<pre><code class=\"cs\">public struct RangeEnumerator {     private readonly int to;     private readonly int step;      private int curr;      public RangeEnumerator(int @from, int to, int step)     {         this.to = to + step;         this.step = step;         this.curr = @from - step;     }      public bool MoveNext()     {         curr += step;         return curr != to;     }          public int Current => curr; }<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0420\u043e\u0437\u043b\u0438\u043d \u0443\u0432\u0438\u0434\u0435\u043b, \u0447\u0442\u043e \u043f\u043e <code>System.Range<\/code> \u043c\u043e\u0436\u043d\u043e \u0444\u043e\u0440\u044b\u0447\u0438\u0442\u044c\u0441\u044f, \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cs\">public static class Extensions {     public static RangeEnumerator GetEnumerator(this Range @this)         => (@this.Start, @this.End) switch         {             ({ IsFromEnd: true, Value: 0 }, { IsFromEnd: true, Value: 0 }) => new RangeEnumerator(0, int.MaxValue, 1),             ({ IsFromEnd: true, Value: 0 }, { IsFromEnd: false, Value: var to }) => new RangeEnumerator(0, to + 1, 1),             ({ IsFromEnd: false, Value: var from }, { IsFromEnd: true, Value: 0 }) => new RangeEnumerator(from, int.MaxValue, 1),             ({ IsFromEnd: false, Value: var from }, { IsFromEnd: false, Value: var to })                 => (from &lt; to) switch                 {                     true => new RangeEnumerator(from, to, 1),                     false => new RangeEnumerator(from, to, -1),                 },             _ => throw new InvalidOperationException(\"Invalid range\")         }; }<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForeachWithRangeEnumerator() {     var a = 0;     foreach (var i in 0..(Iterations - 1))         a += i;     return a; }<\/code><\/pre>\n<p>\u0418 \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0420\u043e\u0437\u043b\u0438\u043d\u043e\u043c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">public int ForeachWithRangeEnumerator() {     int num = 0;     RangeEnumerator enumerator = Extensions.GetEnumerator(new Range(0, Iterations - 1));     while (enumerator.MoveNext())     {         int current = enumerator.Current;         num += current;     }     return num; }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u043d\u0435\u0442 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441 \u0442\u0438\u043f\u043e\u0432, \u0430 \u0442\u0430\u043a \u043a\u0430\u043a <code>RangeEnumerator<\/code> \u043d\u0435 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0442 <code>IDisposable<\/code>, \u0442\u043e \u0438 \u043d\u0435\u0442 try-catch \u0431\u043b\u043e\u043a\u043e\u0432 \u0432\u043e\u043a\u0440\u0443\u0433. \u0412\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>ForWithCustomIncrement<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.477 us<\/p>\n<\/td>\n<td>\n<p>0.0690 us<\/p>\n<\/td>\n<td>\n<p>0.0989 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithEnumerableRange<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>50.501 us<\/p>\n<\/td>\n<td>\n<p>0.9984 us<\/p>\n<\/td>\n<td>\n<p>1.2627 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithYieldReturn<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>53.782 us<\/p>\n<\/td>\n<td>\n<p>1.0583 us<\/p>\n<\/td>\n<td>\n<p>0.9900 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithRangeEnumerator<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>18.061 us<\/p>\n<\/td>\n<td>\n<p>0.1731 us<\/p>\n<\/td>\n<td>\n<p>0.1619 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>18 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u043a\u0443\u043d\u0434 \u043d\u0430 10000 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u041d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0443\u0447\u0448\u0435, \u0447\u0435\u043c \u0430\u043d\u0430\u043b\u043e\u0433\u0438 \u0434\u043e \u044d\u0442\u043e\u0433\u043e, \u043d\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0445\u0443\u0436\u0435, \u0447\u0435\u043c <code>for<\/code>. \u0412\u0440\u0435\u043c\u044f \u0432\u0437\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 \u043a\u043e\u0434\u0433\u0435\u043d!<\/p>\n<p>\u041e\u0434\u043d\u0430 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0446\u0438\u043a\u043b\u0430 <a href=\"https:\/\/sharplab.io\/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKBuIGYACMxgIRgDswALfbKANa4aAbxqMJzJgEsOGRgEkMMKNgzSIHXIxGMA5jAwBuRrkMmAvjXGSADlGkA3NTEaz5Crjv3nTvi4wAvIzkJgD0YYzAAJ6MsNgAJrJ6jABmUBD4adIwADYJYfYQtioY0WiMGNyuAFIKACqMkPi20rkqjADumgDk8sXq+NIAXq7S8hgQbl6yuBhQAK5g6prW1JKMaxsM0\/IAYtAw2DwA6uPcAErYHAYAohwL+Cpq0AAUAJQ2EmLrG5LOUEY2CCjAADEYvn9UodjtxGK8AW5pmCAHQo15KZ4rLSMOAhd6fX5\/P7AgDUwWkEKJf2IAHYgVSNlZqMydsRyEhmKRGLcEMotBotJCfsSJGyOYwrjcYPdHljoIwAOKGWVPVSTKCvKrSbRSgzMYja3CE0V\/QIAPnhxEN3B1KIAyhh+BgKtajSj7gl3qZOuMeJDTSLTaLXroFLg9hl8J6QJVFjAKgA1bC5BYwWOgxgWCphiNRmNxtNJlNpjNZ70WxgcGCdSXXO4PNUvTWgiruFEAWWwCGTqYT+LQAeDElDijzmQL8yLjF7pbBWZzY8jE44CVjqRTZmLfdjiMm5aClurtb1Msb8pbFX3pJCFXI70H1OHI9zy+jq\/Xm\/7s\/TjER6UyBdvHDN9J3jbc50zCwKyPGs62lVUL1eAD8DbORO27H87wfIdh1HED8w\/NIvwg39\/yjIDX0ItdiNyLcZxLMj+EqKZoNw58JErZCKIAHhY71cF9DB\/SfDjJCDMTgynVxK2PeCGzldU3hQq8IGwx9JOHDc6Jk2CT3rM9FObbjMlUio4HvDTNKZKzJIAfUPSpuAyWs5M8ZxcmkBIAHkSnVQVeTAGBbGxV4ACJ3JTLy4gMsKTWfCxGUkZktkkHY5kWZZ5MMpsNWFdj7CcFw4iOBJNFyWJ3BYpLiUK5xlBKxJysquRTGUWwqQKhx6rGVqwAWKAoE60SJHcFQOBTZgUEYAARGBIC0KdlleCAFnkKrrRUxhVvW1rJgqHbdja4L4uHLjNqjMzjtsCt4X6war1tXAUX2py7TmE7hvYsaoAm3JssQpTNQ24gtqq16qo+m72IkqSnpeqZgmva6arh972pBKHUdNd17sBYILsAvEsfYlLaBGqQoggCB\/o7CBHBgAA5GA+Q+GH2I2PHGHJFGObS+kuYAQiRiBseS1LiR2KqAGEBtgVrKzx1GLCAA==\" rel=\"noopener noreferrer nofollow\">\u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 <\/a>\u0442\u0430\u043a:<\/p>\n<pre><code>    L003e: add esi, eax     L0040: mov eax, [rsp+0x38]     L0044: add eax, [rsp+0x34]     L0048: mov [rsp+0x38], eax     L004c: mov eax, [rsp+0x38]     L0050: cmp eax, [rsp+0x30]     L0054: jne short L003a<\/code><\/pre>\n<p>\u0414\u0430, \u0443 \u043d\u0430\u0441 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441-\u0442\u0438\u043f\u043e\u0432 \u043d\u0435\u0442, \u043d\u043e \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0432 \u043f\u0430\u043c\u044f\u0442\u0438! \u0425\u043e\u0442\u044c \u0438 \u043d\u0430 \u0441\u0442\u0435\u043a\u0435. <\/p>\n<h2>\u042d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 4. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u044f\u0447\u0435\u043c \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/h2>\n<p>\u0427\u0442\u043e \u0437\u0430 \u043c\u0430\u0433\u0438\u044f? \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0448 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u043c, \u043a\u0430\u043a <code>for<\/code>, \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u043f\u043e\u043b\u044f \u0432 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b. \u041d\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u0430\u043c \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u043b, \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 \u0431\u044b\u043b\u043e, \u0438 \u043e\u043d \u043d\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u043b \u043d\u0443\u0436\u043d\u044b\u0435. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u043b\u0435\u0433\u0447\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<p>\u0418\u0437 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0430 3:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForeachWithRangeEnumeratorRaw() {     var a = 0;     var enumerator = (0..(Iterations - 1)).GetEnumerator();     while (enumerator.MoveNext())         a += enumerator.Current;     return a; }<\/code><\/pre>\n<p>(\u044f \u0437\u0430\u043c\u0435\u043d\u0438\u043b <code>foreach<\/code> \u043d\u0430 \u0442\u043e\u0447\u043d\u043e \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0441 \u043f\u043e\u0441\u0442\u0444\u0438\u043a\u0441\u043e\u043c <code>Raw<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0435 \u0440\u0443\u0447\u043a\u0430\u043c\u0438, \u0434\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438. \u0421\u043c. \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 3, \u0442\u0430\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e, \u043a\u0430\u043a \u0420\u043e\u0437\u043b\u0438\u043d \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 <code>foreach<\/code>).<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u0441\u043f\u0440\u044f\u0447\u0435\u043c \u0446\u0438\u043a\u043b \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForeachWithRangeEnumeratorRawWithLocal() {     var enumerator = (0..(Iterations - 1)).GetEnumerator();             return EnumerateItAll(enumerator);      static int EnumerateItAll(RangeEnumerator enumerator)     {         var a = 0;         while (enumerator.MoveNext())             a += enumerator.Current;         return a;     } }<\/code><\/pre>\n<p>\u0418 \u043f\u043e\u0431\u0435\u043d\u0447\u0438\u043c:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>Method<\/p>\n<\/th>\n<th>\n<p>Iterations<\/p>\n<\/th>\n<th>\n<p>Mean<\/p>\n<\/th>\n<th>\n<p>Error<\/p>\n<\/th>\n<th>\n<p>StdDev<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>ForWithCustomIncrement<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.498 us<\/p>\n<\/td>\n<td>\n<p>0.0658 us<\/p>\n<\/td>\n<td>\n<p>0.1153 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithEnumerableRange<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>43.313 us<\/p>\n<\/td>\n<td>\n<p>0.8207 us<\/p>\n<\/td>\n<td>\n<p>1.0079 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithYieldReturn<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>56.963 us<\/p>\n<\/td>\n<td>\n<p>1.0638 us<\/p>\n<\/td>\n<td>\n<p>1.0448 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithRangeEnumerator<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>17.931 us<\/p>\n<\/td>\n<td>\n<p>0.2047 us<\/p>\n<\/td>\n<td>\n<p>0.1915 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithRangeEnumeratorRaw<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>17.932 us<\/p>\n<\/td>\n<td>\n<p>0.1486 us<\/p>\n<\/td>\n<td>\n<p>0.1390 us<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ForeachWithRangeEnumeratorRawWithLocal<\/p>\n<\/td>\n<td>\n<p>10000<\/p>\n<\/td>\n<td>\n<p>3.501 us<\/p>\n<\/td>\n<td>\n<p>0.0678 us<\/p>\n<\/td>\n<td>\n<p>0.0807 us<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><code>ForeachWithRangeEnumeratorRawWithLocal<\/code> &#8212; \u044d\u0442\u043e \u043a\u0430\u043a \u0440\u0430\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439. \u0418&#8230; \u043e\u043d \u043f\u043e\u0447\u0442\u0438 \u0432 \u0448\u0435\u0441\u0442\u044c \u0440\u0430\u0437 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u0437\u0430\u0438\u043d\u043b\u0430\u0439\u043d\u0435\u043d\u043d\u044b\u0439! \u041a\u0430\u043a \u0436\u0435 \u0442\u0430\u043a \u0432\u044b\u0448\u043b\u043e-\u0442\u043e?<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u043c <a href=\"https:\/\/sharplab.io\/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKBuIGYACMxgIRgDswALfbKANa4aAbxqMJzJgEsOGRgEkMMKNgzSIHXIxGMA5jAwBuRrkMmAvjXGSADlGkA3NTEaz5Crjv3nTvi4wAvIzkJgD0YYzAAJ6MsNgAJrJ6jABmUBD4adIwADYJYfYQtioY0WiMGNyuAFIKACqMkPi20rkqjADumgDk8sXq+NIAXq7S8hgQbl6yuBhQAK5g6prW1JKMaxsM0\/IAYtAw2DwA6uPcAErYHAYAohwL+Cpq0FedZ1UAMhBg2LkAFABKGwSMTrDaSZxQRicR7PSbQ4L\/AAMADpUf8lPCNFpGHAQoDAaiAOKGe5w1QIoEmCESEEQ4gAdkY5KelJgSgAgrkAbC2S8oICjFtacxyEhdiyHvzlFyef8rjcYKz4dAYdLVYL6RswaKIVDGNggoxkcLwXqJJ1uG1XP8+ZrUQBZCCOGAAORgCAwQOB5otGyNAGpgvbKdBUQBhBZQWByM3+iRMw3x0VWP1pjb0mhpnbEcXMUgsr2cXA44R+3Wi3P5xV3DVh6GkjAqhv\/KrSbS11zEYjt3C+hNBAB8jH+Pb7qIAyhh+BgKuPrbhUfcEoDTJ1xjxtaLK4OJP9dApcHsMvgVyBKosYBUAGp\/BYwC\/IxgWCqH4+n8+Xh+3++Pk0vmugQjhwMCdIwXYtgKKIVO4TrYAgd65D+BJoNu\/oHooH6ZF+8woUhD5Pi+b5YSeOEcAkF6pH8Zi\/sh\/4GpMgHDowoHgZB9bQciFRMYGIQVOQgJoX6g6YUeZFnhRVE0dejAEQx\/BpKexHeOJn5Sd+snyURFhASBYEQdcdYUtB6SZLBcjwYhf4CUJ6EWmJ2GSZRaQyXRhGMAaZlZK+qlOV+1G5LRcl\/hejFTLp9mDsBo7eYwAA8lQQGuuAbhgW4iXuoJRVlmksWxhlKlBVLeTxEC2cJuUWoFZj5QZHEmSVp5lRUcCCZVVWSK+OW0gA+ixVQZOBBWeM4uTSAkADyJSUjitwIGAMC2CsHD\/AARKNfwTXERkwGtA4JhYKYSGmIqJkwcyLMshXGTK0D0ruEL2E4LhxEcCSaLksTuElx1PQ4zjKG9iSfd9cimMothmvZz2A2M4NgNGUDQ5lEjuCoHB\/MwKCMAAIjAkBaHhyz\/BACzyD9PalYwZMU+DkwVLTkpzEtB3+jFY7ENTDMQ6zxr\/IjMY8YuqI8xOLO2EKaz2ejUCY7kN3KpxVKU1zzWSjzP0S2zEKPXqE5McEvG81DPWVCLEvGhLf36yLguIswauZHiJs2110uo1IUQQBACvOq6HpekC9l66K9uMMGrtm0m4cAIRGxAbsnWdDIyODUYxpw8gxfbf0WEAA=\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434\u0433\u0435\u043d<\/a>:<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0437\u0430\u0438\u043d\u043b\u0430\u0439\u043d\u0438\u043b\u0430\u0441\u044c:<\/p>\n<pre><code>Benchmarks.ForeachWithRangeEnumeratorRawWithLocal()     ...     L0044: call Benchmarks.&lt;ForeachWithRangeEnumeratorRawWithLocal>g__EnumerateItAll|8_0(RangeEnumerator)<\/code><\/pre>\n<p>\u0418 \u044d\u0442\u043e \u043d\u0430\u043c \u043d\u0430 \u0440\u0443\u043a\u0443. \u0421\u043c\u043e\u0442\u0440\u0438\u043c \u044d\u0442\u0443 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code>    L0000: mov eax, [rcx]     L0002: mov edx, [rcx+4]     L0005: mov ecx, [rcx+8]     L0008: xor r8d, r8d     L000b: jmp short L0010     L000d: add r8d, ecx         |     L0010: add ecx, edx         |     L0012: cmp ecx, eax         |     L0014: jne short L000d      |     L0016: mov eax, r8d     L0019: ret<\/code><\/pre>\n<p>\u041e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0447\u0435\u0442\u044b\u0440\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0438,<\/p>\n<pre><code>    L000d: add r8d, ecx     L0010: add ecx, edx     L0012: cmp ecx, eax     L0014: jne short L000d<\/code><\/pre>\n<p>\u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u043d\u0430\u0448\u0430 \u043f\u043e\u0431\u0435\u0434\u0430, \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 <code>for<\/code>-\u0430.<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u0442\u0440\u0438 \u0441\u0442\u0440\u043e\u0447\u043a\u0438<\/p>\n<pre><code>    L0000: mov eax, [rcx]     L0002: mov edx, [rcx+4]     L0005: mov ecx, [rcx+8]<\/code><\/pre>\n<p>\u0412\u044b\u0433\u0440\u0443\u0436\u0430\u044e\u0442 \u0432\u0441\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e, \u0432 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b, \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434.<\/p>\n<h2>\u0412\u044b\u0432\u043e\u0434<\/h2>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0445\u043e\u0442\u044c \u0438 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c, \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u043f\u0440\u043e\u0449\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>for<\/code>, \u0447\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0441\u044f \u043b\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u0430\u0448 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u0432 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b.<\/p>\n<p>\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e \u0438\u043d\u043b\u0430\u0439\u043d\u0438\u043d\u0433 \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043b\u0443\u0447\u0448\u0435, \u043d\u043e \u0438 \u0441\u0438\u043b\u044c\u043d\u043e \u0445\u0443\u0436\u0435 (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 3.5 \u0434\u043e 18 us \u043d\u0430 10000 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439).<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<p><strong>UPD<\/strong>: \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u043d\u043e\u0435 \u043d\u0430 \u0432\u0438\u043d\u0434\u0435 \u0438 \u043b\u0438\u043d\u0443\u043a\u0441\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u044f WithLocal \u043c\u0435\u0442\u043e\u0434-\u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a, \u0443 <a class=\"mention\" href=\"\/users\/distortneo\">@DistortNeo<\/a> \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0441\u043e\u0432\u0441\u0435\u043c \u0438\u043d\u043e\u0439 \u043e\u0442 \u0432\u0438\u043d\u0434\u044b \u043a\u043e\u0434\u0433\u0435\u043d. \u0412\u043e\u0442 <a href=\"https:\/\/gist.github.com\/WhiteBlackGoose\/5c567281c0b6bbac816ae0d48607ccba\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a> \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0433\u0438\u0441\u0442 \u0441 \u043a\u043e\u0434\u0433\u0435\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430 WithLocal \u0434\u043b\u044f \u0432\u0438\u043d\u0434\u044b \u0438 \u043b\u0438\u043d\u0443\u043a\u0441\u0430. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e, \u0434\u0436\u0438\u0442 \u0437\u0430\u043c\u0435\u043d\u0438\u043b call \u043d\u0430 jmp, \u0430 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u043b \u043d\u0430 \u0441\u0442\u0435\u043a\u0435.<\/p>\n<h2>\u0421\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p>\u0412\u0435\u0441\u044c \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f \u043f\u0438\u0441\u0430\u043b, \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d \u0432 <a href=\"https:\/\/github.com\/WhiteBlackGoose\/FastForeachEnumeratorArticle\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 github<\/a>.<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/github.com\/WhiteBlackGoose\/FastForeachEnumeratorArticle\/blob\/main\/FastEnum\/Benchmarks\/Program.cs\" rel=\"noopener noreferrer nofollow\">\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/WhiteBlackGoose\/FastForeachEnumeratorArticle\/blob\/main\/FastEnum\/Library\/RangeEnumerator.cs\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u0434 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/WhiteBlackGoose\/FastForeachEnumeratorArticle\/blob\/main\/FastEnum\/Tests\/UnitTest1.cs\" rel=\"noopener noreferrer nofollow\">\u0422\u0435\u0441\u0442\u044b<\/a>, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u043d\u0438\u0435 \u043d\u0430 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/dotnet\/BenchmarkDotNet\" rel=\"noopener noreferrer nofollow\">BenchmarkDotNet<\/a> &#8212; \u0443\u0442\u0438\u043b\u0438\u0442\u0430\/\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0431\u0435\u043d\u0447\u0438\u043d\u0433\u0430<\/p>\n<\/li>\n<\/ul>\n<p>\u0411\u0435\u043d\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0430 \u0432\u0438\u043d\u0434\u0435<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/575664\/\"> https:\/\/habr.com\/ru\/articles\/575664\/<\/a><br \/><\/br><\/br><\/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-400500","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/400500","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=400500"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/400500\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=400500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=400500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=400500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}