{"id":349398,"date":"2023-06-27T09:04:02","date_gmt":"2023-06-27T09:04:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=349398"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=349398","title":{"rendered":"<span>\u0421\u043a\u0430\u0437\u043a\u0430 \u043f\u0440\u043e For vs Foreach<\/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<details class=\"spoiler\">\n<summary>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u0435\u0440\u0438\u044f\u0445<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041c\u0438\u043a\u0440\u043e\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/company\/skbkontur\/blog\/721822\/\">\u0421\u043a\u0430\u0437\u043a\u0430 \u043f\u0440\u043e Method as Parameter<\/a>\u00a0#dotnet #methods #gc<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/723010\/\">\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. PerfView<\/a>\u00a0#performance_analysis #trace #perfview<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/730554\/\">\u041f\u0430\u0440\u043e\u0434\u0438\u044f \u043d\u0430 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f<\/a>\u00a0#dotnet #methods #gc<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/732704\/\">yield return<\/a>\u00a0#dotnet #il-code<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u043e \u0442\u0440\u0435\u0434\u043f\u0443\u043b:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/727532\/\">ThreadPool.Intro<\/a>\u00a0#dotnet #threadpool<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/734670\/\">ThreadPool. async\/await<\/a>\u00a0#dotnet #threadpool #il_code<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/741596\/\">ThreadPool.Chain<\/a> #dotnet #threadpool<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u043e \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u043e\u0435:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/737858\/\">Reciprocal throughput<\/a>\u00a0#microoptimization #low-level<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/740564\/\">\u0421\u043a\u0430\u0437\u043a\u0430 \u043f\u0440\u043e Branch prediction<\/a>\u00a0#microoptimization #low-level<\/p>\n<\/li>\n<\/ol>\n<p>\u0420\u0430\u0437\u043d\u043e\u0435:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/company\/skbkontur\/blog\/661097\/\">\u0421\u043a\u0430\u0437\u043a\u0430 \u043f\u0440\u043e Guid.NewGuid()<\/a>\u00a0#os_specific #dotnet #microoptimization<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/details>\n<p>\u0425\u043e\u0434\u044f\u0442 \u0441\u043b\u0443\u0445\u0438, \u0447\u0442\u043e foreach \u0431\u044b\u0441\u0442\u0440\u0435\u0435 for. \u0410 \u0435\u0449\u0451 \u0445\u043e\u0434\u044f\u0442 \u0441\u043b\u0443\u0445\u0438, \u0447\u0442\u043e for \u0431\u044b\u0441\u0442\u0440\u0435\u0435 foreach. \u041f\u043e\u0440\u0430 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435!<\/p>\n<details class=\"spoiler\">\n<summary>\u0425\u0443\u0434\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043e\u0442\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435, \u0448\u0443\u0442\u043a\u0430 \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0442\u0435\u043c\u0443. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u0437\u043d\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435, \u0447\u0438\u0442\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e.<\/summary>\n<div class=\"spoiler__content\">\n<p>\u2014 \u0421\u0430\u043d\u044f, \u0421\u0430\u043d\u044f, \u0430 \u0434\u0430\u0432\u0430\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043f\u043b\u0430\u0432\u0430\u0435\u0442? \u041c\u043e\u0439 \u0441\u0430\u043c\u043e\u043b\u0451\u0442 \u0438\u043b\u0438 \u0442\u0432\u043e\u0439 \u043f\u043e\u0435\u0437\u0434?<\/p>\n<p>\u2014 \u041e\u0433\u043e, \u043f\u043e\u0448\u043b\u0438 \u043d\u0430 \u0440\u0435\u0447\u043a\u0443, \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c!<\/p>\n<p>\u2026<\/p>\n<p>\u2014 \u0425\u0430, \u0442\u0432\u043e\u0439 \u043f\u043e\u0435\u0437\u0434 \u0443\u0442\u043e\u043d\u0443\u043b \u0440\u0430\u043d\u044c\u0448\u0435 \u043c\u043e\u0435\u0433\u043e \u0441\u0430\u043c\u043e\u043b\u0451\u0442\u0430!<\/p>\n<p>\u2014 \u041d\u0443 \u0438 \u043b\u0430\u0434\u043d\u043e. \u0410 \u0434\u0430\u0432\u0430\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043b\u0435\u0442\u0430\u0435\u0442? \u0423\u0442\u044e\u0433 \u0438\u043b\u0438 \u043a\u0438\u0440\u043f\u0438\u0447?<\/p>\n<p>\u2014 \u0414\u0430\u0432\u0430\u0439, \u0441\u0435\u0439\u0447\u0430\u0441 \u044f \u0443\u0442\u044e\u0433 \u0438\u0437 \u0434\u043e\u043c\u0430 \u0432\u043e\u0437\u044c\u043c\u0443, \u0430 \u0442\u044b \u043a\u0438\u0440\u043f\u0438\u0447 \u0441\u043e \u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432\u043e\u0437\u044c\u043c\u0438<\/p>\n<p>\u2026<\/p>\n<p>\u2014 \u041a\u0438\u0434\u0430\u0435\u043c, \u0440\u0430\u0437, \u0434\u0432\u0430, \u0442\u0440\u0438!<\/p>\n<p>\u2014 \u041e\u0433\u043e, \u0443\u0442\u044e\u0433 \u0434\u0430\u043b\u044c\u0448\u0435 \u0443\u043b\u0435\u0442\u0435\u043b. \u042d\u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0443 \u043d\u0435\u0433\u043e \u0444\u043e\u0440\u043c\u0430 \u0430\u044d\u0440\u043e\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f!<\/p>\n<p>\u2014 \u0414\u0430\u0432\u0430\u0439 \u0435\u0449\u0451 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c. \u041a\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0448\u044c, \u0438\u0437 \u0447\u0435\u0433\u043e \u043b\u0443\u0447\u0448\u0435 \u043f\u0430\u0440\u0430\u0448\u044e\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f, \u0438\u0437 \u043f\u0440\u043e\u0441\u0442\u044b\u043d\u0438 \u0438\u043b\u0438 \u0441\u043a\u0430\u0442\u0435\u0440\u0442\u0438? \u041c\u043e\u0436\u043d\u043e \u0441 \u0447\u0435\u0440\u0434\u0430\u043a\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c.<\/p>\n<p>\u2026<\/p>\n<p>\u2014 \u042d\u0439, \u0431\u0435\u0437\u0434\u0430\u0440\u0438, \u0447\u0435\u0433\u043e \u0431\u0435\u0437 \u0434\u0435\u043b\u0430 \u0448\u043b\u044f\u0435\u0442\u0435\u0441\u044c? \u0410 \u043d\u0443 \u0443\u0440\u043e\u043a\u0438 \u0434\u0435\u043b\u0430\u0442\u044c!<\/p>\n<p>\u2014 \u041d\u0443 \u043c\u0430\u0430\u0430\u043c. \u041c\u044b \u0442\u0443\u0442 \u0432\u043e\u043e\u0431\u0449\u0435-\u0442\u043e \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u0441\u044f.<\/p>\n<p>\u2014 \u0423\u0440\u043e\u043a\u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u0448\u044c, \u043f\u043e\u0442\u043e\u043c \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u0448\u044c. \u041a\u0441\u0442\u0430\u0442\u0438, \u0442\u044b \u0443\u0442\u044e\u0433 \u043d\u0435 \u0442\u0440\u043e\u0433\u0430\u043b? \u041d\u0438\u043a\u0430\u043a \u043d\u0430\u0439\u0442\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443.<\/p>\n<\/div>\n<\/details>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u0430\u0438\u043c\u043e\u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c for \u0438 foreach. \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u044b. \u0422\u0430\u043a\u0436\u0435, \u044d\u0442\u043e \u0441\u043f\u0438\u0441\u043a\u0438 \u2014 List. \u0410 \u0435\u0449\u0451, \u0441\u044e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043d\u0435\u0441\u0442\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 \u2014 IList. \u041f\u043e\u0434 \u044d\u0442\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u0440\u044f\u0442\u0430\u0442\u044c \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0439 List, \u0442\u0430\u043a \u0438 \u043c\u0430\u0441\u0441\u0438\u0432 (\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c). \u0412\u0441\u044f\u043a\u0438\u0435 \u0438\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043b\u043e\u0432\u0430\u0440\u0438, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0442\u0440\u043e\u0438\u0442\u044c for \u043f\u043e \u043a\u043b\u044e\u0447\u0430\u043c-int&#8217;\u0430\u043c, \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c.<\/p>\n<p>\u0417\u0430\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u043d\u0430\u0448\u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"cs\">public class ForeachVsFor {     private List&lt;int> list; \/\/\u0421\u043f\u0438\u0441\u043e\u043a     private IList&lt;int> iListOverList; \/\/\u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList     private int[] array; \/\/\u041c\u0430\u0441\u0441\u0438\u0432     private IList&lt;int> iListOverArray; \/\/\u041c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList       [GlobalSetup]     public void Setup()     {         var length = 45;\/\/It's a good number. Not too small, not too big.         var ints = new List&lt;int>(length);         for (var i = 0; i &lt; length; i++)         {             ints.Add(i);         }         list = ints;         iListOverList = ints;         array = ints.ToArray();         iListOverArray = array;     } }<\/code><\/pre>\n<p>\u0414\u0430\u0431\u044b \u0438\u0441\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u044b\u0439 \u0446\u0438\u043a\u043b \u043d\u0435 \u0431\u044b\u043b \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0431\u0435\u0437 \u0442\u0435\u043b\u0430, \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u2014 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0411\u0443\u0434\u0435\u043c \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. \u0410 \u0435\u0449\u0451, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0435\u0440\u0447\u0438\u043d\u043a\u0438, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0438 \u043d\u0430 Net6, \u0438 \u043d\u0430 Net7!<\/p>\n<h3>\u041c\u0430\u0441\u0441\u0438\u0432\u044b<\/h3>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u043e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043a \u0441\u043b\u043e\u0436\u043d\u043e\u043c\u0443, \u0442\u043e \u0435\u0441\u0442\u044c \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432. \u041a\u0430\u043a \u0432\u044b \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForArray() {     var summ = 0;     for (int i = 0; i &lt; array.Length; i++)     {         summ += array[i];     }     return summ; }   [Benchmark] public int ForeachArray() {     var summ = 0;     foreach (var e in array)     {         summ += e;     }     return summ; }   [Benchmark] \/\/\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u0434\u0435\u0441\u044c \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043f\u043e\u043c\u043d\u0438\u0442 \u043f\u0440\u043e safety-check'\u0438 \/\/\u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/737858\/). public unsafe int ForUnsafeArray() {     var summ = 0;     var length = array.Length;     fixed (int* ptr = array)     {         var pointer = ptr;         var bound = pointer + length;         while (pointer != bound)         {             summ += *pointer;             pointer += 1;         }     }     return summ; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>|                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 6.0 |  25.52 ns |      - |         - | |          ForeachArray | .NET 6.0 |  15.87 ns |      - |         - | |        ForUnsafeArray | .NET 6.0 |  17.75 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 7.0 |  24.37 ns |      - |         - | |          ForeachArray | .NET 7.0 |  15.71 ns |      - |         - | |        ForUnsafeArray | .NET 7.0 |  18.22 ns |      - |         - |<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e foreach \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 for \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445, \u043f\u0440\u0438\u0447\u0435\u043c \u043d\u0430\u043c\u043d\u043e\u0433\u043e.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, ForUnsafe \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e For. \u0412 \u0447\u0435\u043c \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 For \u0438 ForUnsafe <a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/737858\/\">\u043c\u044b \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u043c<\/a> \u2014 \u0432 \u043b\u0438\u0448\u043d\u0435\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u043d\u0430 \u043d\u0435\u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u043d\u0438\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438.<\/p>\n<p>\u0418 ForUnsafe \u043b\u0438\u0448\u044c \u0447\u0443\u0442\u044c-\u0447\u0443\u0442\u044c \u0445\u0443\u0436\u0435 Foreach. \u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0437\u0443\u0447\u0430\u0442\u044c \u044d\u0442\u0443 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u043f\u043e\u0434 \u043c\u0438\u043a\u0440\u043e\u0441\u043a\u043e\u043f\u043e\u043c \u0432 ASM-\u043a\u043e\u0434\u0435.<\/p>\n<p>\u041c\u0435\u0436\u0434\u0443 Net6 \u0438 Net7 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f.<\/p>\n<\/div>\n<\/details>\n<h3>\u0421\u043f\u0438\u0441\u043a\u0438<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0441\u043f\u0438\u0441\u043a\u0430\u043c, \u0442\u043e \u0435\u0441\u0442\u044c List. \u0423 \u043d\u0438\u0445 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c, \u043a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0442\u043e\u0436\u0435 \u043c\u0430\u0441\u0441\u0438\u0432. \u041d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0438 \u0435\u0433\u043e, \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0443 \u043d\u0435\u0433\u043e \u0441\u0432\u043e\u0438. \u041a\u0430\u043a \u0432\u044b \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForList() {     var summ = 0;     for (int i = 0; i &lt; list.Count; i++)     {         summ += list[i];     }     return summ; }   [Benchmark] public int ForeachList() {     var summ = 0;     foreach (var e in list)     {         summ += e;     }     return summ; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0434\u043b\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>|                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |               ForList | .NET 6.0 |  32.96 ns |      - |         - | |           ForeachList | .NET 6.0 |  49.89 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |               ForList | .NET 7.0 |  32.25 ns |      - |         - | |           ForeachList | .NET 7.0 |  36.08 ns |      - |         - |   \u0421\u0442\u0430\u0440\u044b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0438 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434 \u0433\u043b\u0430\u0437\u0430\u043c\u0438 (\u0442\u043e\u043b\u044c\u043a\u043e Net7) |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 7.0 |  24.37 ns |      - |         - | |          ForeachArray | .NET 7.0 |  15.71 ns |      - |         - |<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 for \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 foreach, \u043f\u0440\u0438\u0447\u0435\u043c \u043d\u0430\u043c\u043d\u043e\u0433\u043e (\u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0431\u044b\u043b\u043e \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442!). \u041d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 Net6! \u0410 \u043d\u0430 Net7 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0443\u0436\u0435 \u043d\u0435 \u0442\u0430\u043a\u0430\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f, \u0445\u043e\u0442\u044f \u0438 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442. \u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0437\u0443\u0447\u0430\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0432 Net7 \u043b\u0443\u0447\u0448\u0435. \u041b\u0443\u0447\u0448\u0435 \u2014 \u0438 \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<p>\u041e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u043e\u0431\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u044e\u0442 \u043e\u0431\u043e\u0438\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u043c \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430.<\/p>\n<\/div>\n<\/details>\n<h3>\u041c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0438\u043f\u0430 IList, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u043b\u0438 \u043d\u0430\u0448 \u043c\u0430\u0441\u0441\u0438\u0432. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u043b\u0438 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u0430 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 IList.<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForIListOverArray() {     var summ = 0;     for (int i = 0; i &lt; iListOverArray.Count; i++)     {         summ += iListOverArray[i];     }     return summ; }   [Benchmark] public int ForeachIListOverArray() {     var summ = 0;     foreach (var e in iListOverArray)     {         summ += e;     }     return summ; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e\u0434 IList \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>|                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |     ForIListOverArray | .NET 6.0 | 210.78 ns |      - |         - | | ForeachIListOverArray | .NET 6.0 | 197.25 ns | 0.0076 |      32 B | |---------------------- |--------- |----------:|-------:|----------:| |     ForIListOverArray | .NET 7.0 | 220.61 ns |      - |         - | | ForeachIListOverArray | .NET 7.0 | 230.32 ns | 0.0076 |      32 B |   \u0421\u0442\u0430\u0440\u044b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0438, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434 \u0433\u043b\u0430\u0437\u0430\u043c\u0438 (\u0442\u043e\u043b\u044c\u043a\u043e Net7) |---------------------- |--------- |----------:|-------:|----------:| |               ForList | .NET 7.0 |  32.25 ns |      - |         - | |           ForeachList | .NET 7.0 |  36.08 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 7.0 |  24.37 ns |      - |         - | |          ForeachArray | .NET 7.0 |  15.71 ns |      - |         - |<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e foreach \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u0443\u0441\u043e\u0440\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438!<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0435, \u0447\u0442\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u2014 foreach \u043d\u0430 Net7 \u0441\u0442\u0430\u043b \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u043d\u0430 Net6!<\/p>\n<p>\u041d\u0443 \u0438 \u0442\u0440\u0435\u0442\u044c\u0435, \u0447\u0442\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u2014 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a IList&#8217;\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u043a \u0441\u043f\u0438\u0441\u043a\u0443 \u0438\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0443!<\/p>\n<\/div>\n<\/details>\n<h3>\u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList<\/h3>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u0438\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0438\u043f\u0430 IList, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u043b\u0438 \u043d\u0430\u0448 \u0441\u043f\u0438\u0441\u043e\u043a. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u043b\u0438 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c, \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u0430 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 IList.<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForIListOverList() {     var summ = 0;     for (int i = 0; i &lt; iListOverList.Count; i++)     {         summ += iListOverList[i];     }     return summ; }   [Benchmark] public int ForeachIListOverList() {     var summ = 0;     foreach (var e in iListOverList)     {         summ += e;     }     return summ; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0434\u043b\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0434 IList \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>|                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |      ForIListOverList | .NET 6.0 | 190.77 ns |      - |         - | |  ForeachIListOverList | .NET 6.0 | 309.41 ns | 0.0095 |      40 B | |---------------------- |--------- |----------:|-------:|----------:| |      ForIListOverList | .NET 7.0 | 187.43 ns |      - |         - | |  ForeachIListOverList | .NET 7.0 | 355.90 ns | 0.0095 |      40 B |   \u0421\u0442\u0430\u0440\u044b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0438, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434 \u0433\u043b\u0430\u0437\u0430\u043c\u0438 (\u0442\u043e\u043b\u044c\u043a\u043e Net7) |---------------------- |--------- |----------:|-------:|----------:| |               ForList | .NET 7.0 |  32.25 ns |      - |         - | |           ForeachList | .NET 7.0 |  36.08 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 7.0 |  24.37 ns |      - |         - | |          ForeachArray | .NET 7.0 |  15.71 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |     ForIListOverArray | .NET 7.0 | 220.61 ns |      - |         - | | ForeachIListOverArray | .NET 7.0 | 230.32 ns | 0.0076 |      32 B |<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, foreach \u043c\u0443\u0441\u043e\u0440\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438; foreach \u043d\u0430 Net7 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u043d\u0430 Net6; \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a IList&#8217;\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u043a \u0441\u043f\u0438\u0441\u043a\u0443 \u0438\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0443.<\/p>\n<p>\u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c\u044b\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u044b: Foreach \u043d\u0430 IList&#8217;\u0435 \u043f\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c List \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u043d\u0430 \u0442\u043e\u043c IList, \u043f\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c array. \u0410 \u0434\u043b\u044f For \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442.<\/p>\n<\/div>\n<\/details>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043d\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a IList<\/h3>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u0431\u044b\u043b \u0431\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0431\u044a\u0435\u043c\u043d\u044b\u043c. \u041d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0448\u0430\u0436\u043e\u043a. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 IL-\u043a\u043e\u0434, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043c\u0435\u0442\u043e\u0434\u0430 ForeachIListOverList.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/558\/357\/52a\/55835752a5868a9d4c1c14ac91173941.png\" width=\"1600\" height=\"885\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/558\/357\/52a\/55835752a5868a9d4c1c14ac91173941.png\"\/><\/figure>\n<p>\u041a\u0440\u0430\u0441\u043d\u044b\u043c \u0432\u044b\u0434\u0435\u043b\u0435\u043d \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043e\u0442 \u043e\u0434\u043d\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0446\u0438\u043a\u043b\u0430 \u043a \u0434\u0440\u0443\u0433\u043e\u0439. \u0421\u0438\u043d\u0438\u043c \u2014 \u0432\u044b\u0437\u043e\u0432\u044b \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 GetCurrent \u0438 MoveNext \u0443 enumerator&#8217;\u0430. \u041a\u043e\u0442\u043e\u0440\u044b\u0439 \u0447\u0443\u0442\u044c \u0432\u044b\u0448\u0435 \u043c\u044b \u0438 \u0441\u043e\u0437\u0434\u0430\u043b\u0438, \u0447\u0435\u043c \u0438 \u043d\u0430\u043c\u0443\u0441\u043e\u0440\u0438\u043b\u0438 \u2014 \u044f \u0432\u044b\u0434\u0435\u043b\u0438\u043b \u044d\u0442\u043e \u0437\u0435\u043b\u0451\u043d\u044b\u043c \u0446\u0432\u0435\u0442\u043e\u043c \u0438 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u043d\u0443\u043b, \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b\u0441\u044f \u0442\u0430\u043c \u043a\u043b\u0430\u0441\u0441, \u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0430 \u0445\u0438\u043f\u0435.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 IL-\u043a\u043e\u0434 \u043c\u0435\u0442\u043e\u0434\u0430 ForeachList.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8e4\/32b\/8de\/8e432b8ded5382cb5fb4615900f22897.png\" width=\"1600\" height=\"838\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8e4\/32b\/8de\/8e432b8ded5382cb5fb4615900f22897.png\"\/><\/figure>\n<p>\u041a\u0440\u0430\u0441\u043d\u044b\u043c \u0432\u044b\u0434\u0435\u043b\u0435\u043d \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043e\u0442 \u043e\u0434\u043d\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0446\u0438\u043a\u043b\u0430 \u043a \u0434\u0440\u0443\u0433\u043e\u0439. \u0421\u0438\u043d\u0438\u043c \u2014 \u0432\u044b\u0437\u043e\u0432\u044b\u00a0<strong>\u0443\u0436\u0435 \u043d\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445, \u0430 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432<\/strong>\u00a0<code>GetCurrent<\/code>\u00a0\u0438\u00a0<code>MoveNext<\/code>\u00a0\u0443 enumerator&#8217;\u0430. \u041a\u043e\u0442\u043e\u0440\u044b\u0439 \u0447\u0443\u0442\u044c \u0432\u044b\u0448\u0435 \u043c\u044b \u0438 \u0441\u043e\u0437\u0434\u0430\u043b\u0438. \u041d\u043e \u043c\u044b \u0438\u043c \u043d\u0435 \u043d\u0430\u043c\u0443\u0441\u043e\u0440\u0438\u043b\u0438 \u2014 \u044f \u0432\u044b\u0434\u0435\u043b\u0438\u043b \u044d\u0442\u043e \u0437\u0435\u043b\u0451\u043d\u044b\u043c \u0446\u0432\u0435\u0442\u043e\u043c \u0438 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u043d\u0443\u043b, \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b\u0441\u044f \u0442\u0430\u043c valuetype, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043d\u0430 \u0441\u0442\u0435\u043a\u0435. \u0414\u0430, \u0434\u043e\u0442\u043d\u0435\u0442 \u0443\u043c\u043d\u044b\u0439 \u0438 \u043a\u043e\u0433\u0434\u0430 \u0443\u0432\u0435\u0440\u0435\u043d \u0432 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u0434\u043b\u044f \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e List&#8217;\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043b\u0435\u0433\u043a\u043e\u0432\u0435\u0441\u043d\u044b\u0439 Enumerator, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430.<\/p>\n<p>\u0412 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u043c \u043a\u043e\u0434 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439. \u0418\u0437 \u0447\u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442, \u0447\u0442\u043e \u0432\u043e\u0442 \u044d\u0442\u0438 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u043e\u0447\u0435\u043d\u044c \u0434\u043e\u0440\u043e\u0433\u0438\u0435. \u0418 \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u0430\u043a. \u042d\u0442\u043e \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u043d\u044f\u0442\u044c, \u0435\u0441\u043b\u0438 \u0443\u0433\u043b\u0443\u0431\u0438\u0442\u044c\u0441\u044f \u0432 \u0442\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b. \u0421\u0435\u0439\u0447\u0430\u0441 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c, \u0438\u043d\u0430\u0447\u0435 \u0440\u0430\u0437\u043c\u0435\u0440 \u0441\u0442\u0430\u0442\u044c\u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u0441\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437.<\/p>\n<h4>\u0427\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c<\/h4>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 foreach \u0443 \u043d\u0430\u0441 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u044b\u0439\u0434\u0435\u0442. \u041c\u0435\u0442\u043e\u0434\u0430\u00a0<code>bool TryMoveNext(out T value)<\/code>, \u0443\u0432\u044b, \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442. \u041d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043a\u043e\u0434 \u0441 for \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 IList&#8217;\u043e\u043c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430 ForIListOverList.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4ec\/e04\/1a7\/4ece041a709f0dfe94aa94d199c703c8.png\" width=\"1531\" height=\"846\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4ec\/e04\/1a7\/4ece041a709f0dfe94aa94d199c703c8.png\"\/><\/figure>\n<p>\u041a\u0440\u0430\u0441\u043d\u044b\u043c \u043e\u0442\u043c\u0435\u0447\u0435\u043d \u0446\u0438\u043a\u043b. \u0421\u0438\u043d\u0438\u043c \u2014 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u043c\u0435\u0442\u043e\u0434\u043e\u0432. GetItem \u2014 \u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u044f. \u0410 \u0432\u043e\u0442 GetCount \u2014 \u043f\u043e\u0445\u043e\u0436\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u0432\u044b\u0437\u043e\u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 .Count! \u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043e\u0442\u043a\u0443\u0434\u0430 \u0434\u043e\u0442\u043d\u0435\u0442\u0443 \u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u0434 \u043d\u0438\u043c \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u0432\u044b\u0437\u043e\u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 Count? \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0434\u043e\u0440\u043e\u0433\u043e\u0439 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 Count. \u041d\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u0438\u043c \u0438 \u0437\u0430\u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u00ab\u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c\u00bb \u0435\u0451 \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0446\u0438\u043a\u043b\u0430.<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForIListOverList() {     var summ = 0;     for (int i = 0; i &lt; iListOverList.Count; i++)     {         summ += iListOverList[i];     }     return summ; }   [Benchmark] public int ForIListOverList2() {     var summ = 0;     var upperBound = iListOverList.Count;     for (int i = 0; i &lt; upperBound; i++)     {         summ += iListOverList[i];     }     return summ; }<\/code><\/pre>\n<p>\u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u043a\u043e\u0434 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0438 \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u044f iListOverArray. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043b\u0438.<\/p>\n<pre><code>|                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |      ForIListOverList | .NET 6.0 | 190.77 ns |      - |         - | |     ForIListOverList2 | .NET 6.0 | 115.69 ns |      - |         - | |     ForIListOverArray | .NET 6.0 | 210.78 ns |      - |         - | |    ForIListOverArray2 | .NET 6.0 | 125.25 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |      ForIListOverList | .NET 7.0 | 187.43 ns |      - |         - | |     ForIListOverList2 | .NET 7.0 | 113.02 ns |      - |         - | |     ForIListOverArray | .NET 7.0 | 220.61 ns |      - |         - | |    ForIListOverArray2 | .NET 7.0 | 124.65 ns |      - |         - |<\/code><\/pre>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442! \u0412\u0435\u0440\u0441\u0438\u0438 \u0441 \u0446\u0438\u0444\u0435\u0440\u043a\u043e\u0439 2, \u0442\u0435, \u0433\u0434\u0435 \u043c\u044b \u0437\u0430\u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u0415\u0441\u043b\u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u044c IL-\u043a\u043e\u0434, \u0442\u043e \u0432\u044b \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0435\u043b\u0430 \u0446\u0438\u043a\u043b\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u043f\u0430\u043b \u0432\u044b\u0437\u043e\u0432 GetCount. \u041e\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0434\u043e \u0446\u0438\u043a\u043b\u0430 \u0438 \u043a\u0435\u0448\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043c\u044b \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u0432 C#-\u043a\u043e\u0434\u0435. \u0418 \u0437\u0430\u043c\u0435\u0442\u043d\u043e, \u0447\u0442\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0432 \u0447\u0438\u0441\u043b\u043e \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430, \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u0430\u0441\u044c \u0442\u043e\u0436\u0435 \u043f\u043e\u0447\u0442\u0438 \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430, \u0447\u0442\u043e \u0435\u0449\u0451 \u0440\u0430\u0437 \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0435\u0442 \u0438\u0445 \u0432\u044b\u0441\u043e\u043a\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c.<\/p>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 \u043d\u0430 Net7 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a IList&#8217;\u0443 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0447\u0435\u043c \u043d\u0430 Net6?<\/h3>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0443 \u043c\u0435\u043d\u044f \u043d\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0442\u0430. \u0415\u0441\u043b\u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c ASM-\u043a\u043e\u0434 \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u043d\u0430 Net6 \u0438 Net7, \u043a\u0440\u043e\u043c\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0442\u043e \u043e\u043d \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d (\u043d\u0430 99%, \u043d\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043d\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0437\u043d\u0430\u0447\u0438\u043c\u044b\u043c\u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438). \u0422\u043e \u0435\u0441\u0442\u044c \u0432\u0441\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u0433\u0434\u0435-\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0434\u043e\u0442\u043d\u0435\u0442\u0430 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438 \u0438\\\u0438\u043b\u0438 \u044d\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c\u0438. \u042d\u0442\u0430 \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u043e\u043d\u0430 \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u0438\u0448\u044c IList&#8217;\u043e\u043c. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\n<p>\u041a\u043e\u0434 \u0441 For-\u0446\u0438\u043a\u043b\u0430\u043c\u0438 \u043f\u043e IList&#8217;\u0443 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u043d\u0430 Net6 \u0438 Net7;<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0434 \u0441 Foreach-\u0446\u0438\u043a\u043b\u0430\u043c\u0438 \u043f\u043e IList&#8217;\u0443 \u043d\u0430 Net7 \u043e\u0449\u0443\u0442\u0438\u043c\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0412\u0435\u0441\u044c \u043f\u0440\u043e\u0447\u0438\u0439 \u043a\u043e\u0434 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u0430\u043c\u0438 \u0438\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438, \u0447\u0442\u043e for, \u0447\u0442\u043e foreach, \u0431\u0435\u0437 IList&#8217;\u0430, \u043b\u0438\u0431\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432 \u043d\u0430 Net6 \u0438 Net7, \u043b\u0438\u0431\u043e Net7 \u0447\u0443\u0442\u044c-\u0447\u0443\u0442\u044c \u043f\u043e\u0431\u044b\u0441\u0442\u0440\u0435\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u043a\u0442\u043e-\u0442\u043e \u0437\u043d\u0430\u0435\u0442 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u044d\u0442\u043e\u0439 \u0434\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u0438, \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0438\u043c\u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445!<\/p>\n<h3>\u041f\u043e\u043b\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430<\/h3>\n<pre><code>\/\/ * Summary *   BenchmarkDotNet=v0.13.4, OS=Windows 10 (10.0.19045.2486) Intel Core i7-4771 CPU 3.50GHz (Haswell), 1 CPU, 8 logical and 4 physical cores .NET SDK=7.0.102   [Host]   : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT AVX2   .Net 6.0 : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT AVX2   .Net 7.0 : .NET 7.0.2 (7.0.222.60605), X64 RyuJIT AVX2   |                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |      ForIListOverList | .NET 6.0 | 190.77 ns |      - |         - | |     ForIListOverList2 | .NET 6.0 | 115.69 ns |      - |         - | |  ForeachIListOverList | .NET 6.0 | 309.41 ns | 0.0095 |      40 B | |               ForList | .NET 6.0 |  32.96 ns |      - |         - | |           ForeachList | .NET 6.0 |  49.89 ns |      - |         - | |              ForArray | .NET 6.0 |  25.52 ns |      - |         - | |          ForeachArray | .NET 6.0 |  15.87 ns |      - |         - | |        ForUnsafeArray | .NET 6.0 |  17.75 ns |      - |         - | |     ForIListOverArray | .NET 6.0 | 210.78 ns |      - |         - | |    ForIListOverArray2 | .NET 6.0 | 125.25 ns |      - |         - | | ForeachIListOverArray | .NET 6.0 | 197.25 ns | 0.0076 |      32 B | |      ForIListOverList | .NET 7.0 | 187.43 ns |      - |         - | |     ForIListOverList2 | .NET 7.0 | 113.02 ns |      - |         - | |  ForeachIListOverList | .NET 7.0 | 355.90 ns | 0.0095 |      40 B | |               ForList | .NET 7.0 |  32.25 ns |      - |         - | |           ForeachList | .NET 7.0 |  36.08 ns |      - |         - | |              ForArray | .NET 7.0 |  24.37 ns |      - |         - | |          ForeachArray | .NET 7.0 |  15.71 ns |      - |         - | |        ForUnsafeArray | .NET 7.0 |  18.22 ns |      - |         - | |     ForIListOverArray | .NET 7.0 | 220.61 ns |      - |         - | |    ForIListOverArray2 | .NET 7.0 | 124.65 ns |      - |         - | | ForeachIListOverArray | .NET 7.0 | 230.32 ns | 0.0076 |      32 B |<\/code><\/pre>\n<h3>\u0412\u044b\u0432\u043e\u0434\u044b<\/h3>\n<p>\u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435<\/p>\n<ul>\n<li>\n<p>foreach \u0431\u044b\u0441\u0442\u0440\u0435\u0435 for \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445, \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u043d\u0430 int[]. \u041d\u043e unsafe-\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f for \u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0433\u043d\u0430\u0442\u044c foreach.<\/p>\n<\/li>\n<li>\n<p>for \u0431\u044b\u0441\u0442\u0440\u0435\u0435 foreach \u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430\u0445, \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u043d\u0430\u00a0<code>List&lt;int><\/code>. \u0425\u043e\u0442\u044f, \u043d\u0430 Net7 foreach \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u043b\u0438. \u041d\u043e for \u043e\u043d \u0442\u0430\u043a \u0438 \u043d\u0435 \u0434\u043e\u0433\u043d\u0430\u043b.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a IList&#8217;\u0443 \u043a\u0430\u043a \u043a \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0447\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e for, \u0447\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e foreach, \u043e\u0447\u0435\u043d\u044c \u0434\u043e\u0440\u043e\u0433\u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437-\u0437\u0430 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c, foreach \u043d\u0430 IList&#8217;\u0435 \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u044d\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440\u0430. \u0410 \u0435\u0449\u0451, foreach \u043d\u0430 IList&#8217;\u0435 \u0434\u0435\u0433\u0440\u0430\u0434\u0438\u0440\u0443\u0435\u0442 \u043d\u0430 Net7 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 Net6.<\/p>\n<\/li>\n<\/ul>\n<p>\u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435<\/p>\n<ul>\n<li>\n<p>\u0412 \u043c\u0435\u0441\u0442\u0430\u0445,\u00a0<strong>\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e<\/strong>\u00a0\u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043a \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043d\u0435 \u043c\u0435\u0441\u0442\u043e \u044d\u0442\u0438\u043c \u0432\u0441\u044f\u043a\u0438\u043c \u041e\u041e\u041f\u0448\u043d\u044b\u043c \u0448\u0442\u0443\u0447\u043a\u0430\u043c \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438. \u0412\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u043a\u0440\u0430\u0439\u043d\u0435 \u0434\u043e\u0440\u043e\u0433\u0438. \u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 \u043d\u0438\u043a\u0443\u0434\u0430 \u043d\u0435 \u0434\u0435\u0442\u044c\u0441\u044f, \u0441\u0442\u0430\u0440\u0430\u0439\u0442\u0435\u0441\u044c \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0418\u043d\u0430\u0447\u0435 \u043b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e 90% \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043f\u043e\u0442\u0440\u0430\u0447\u0435\u043d\u043e \u043d\u0435 \u043d\u0430 \u0432\u0430\u0448 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u043a\u043e\u0434, \u0430 \u043d\u0430 \u043f\u0440\u043e\u0434\u0435\u043b\u043a\u0438 \u0434\u043e\u0442\u043d\u0435\u0442\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0412\u043f\u0440\u043e\u0447\u0435\u043c, \u0435\u0441\u043b\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442\u044c \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438 (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0441\u0432\u043e\u0438\u043c\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438, \u043d\u0435 \u0434\u043e\u0442\u043d\u0435\u0442\u043d\u044b\u043c\u0438), \u0442\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043a\u043e\u0435-\u0447\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c. \u0412 \u0442\u043e\u043c \u0436\u0435 Net7 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043a\u0435 sealed-\u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0441 PGO.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u0447\u0442\u043e \u0447\u0442\u043e-\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0447\u0435\u0433\u043e-\u0442\u043e, \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0434\u044f\u00a0<strong>\u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u0432 \u043d\u0430 \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u043d\u044b\u0445 \u043a \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0438<\/strong>. \u0420\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0447\u0435\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0435\u0431\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c. \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0432 \u043e\u0434\u043d\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u043f\u043e\u0431\u0435\u0436\u0434\u0430\u0435\u0442 \u043e\u0434\u043d\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0430 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u0434\u0440\u0443\u0433\u043e\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u0441\u0451 \u043d\u043e\u0432\u043e\u0435 \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u0442\u0430\u0440\u043e\u0433\u043e. \u0418\u043d\u043e\u0433\u0434\u0430, \u0434\u0430\u0436\u0435 \u0432 \u0442\u0430\u043a\u0438\u0445 \u0432\u0435\u0449\u0430\u0445 \u043a\u0430\u043a DotNet, \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u043e\u0440\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0447\u0435\u043c-\u0442\u043e \u0436\u0435\u0440\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u0443\u0433\u043e\u0434\u0443 \u0434\u0440\u0443\u0433\u0438\u043c \u0446\u0435\u043b\u044f\u043c, \u0432\u044b\u043f\u0443\u0441\u043a\u0430\u044f \u043d\u043e\u0432\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438. \u0418 \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u0430 \u043a \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0441\u0442\u043e\u0438\u0442 \u0442\u0449\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\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\/companies\/skbkontur\/articles\/743454\/\"> https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/743454\/<\/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<details class=\"spoiler\">\n<summary>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u0435\u0440\u0438\u044f\u0445<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041c\u0438\u043a\u0440\u043e\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/company\/skbkontur\/blog\/721822\/\">\u0421\u043a\u0430\u0437\u043a\u0430 \u043f\u0440\u043e Method as Parameter<\/a>\u00a0#dotnet #methods #gc<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/723010\/\">\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. PerfView<\/a>\u00a0#performance_analysis #trace #perfview<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/730554\/\">\u041f\u0430\u0440\u043e\u0434\u0438\u044f \u043d\u0430 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f<\/a>\u00a0#dotnet #methods #gc<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/732704\/\">yield return<\/a>\u00a0#dotnet #il-code<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u043e \u0442\u0440\u0435\u0434\u043f\u0443\u043b:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/727532\/\">ThreadPool.Intro<\/a>\u00a0#dotnet #threadpool<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/734670\/\">ThreadPool. async\/await<\/a>\u00a0#dotnet #threadpool #il_code<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/741596\/\">ThreadPool.Chain<\/a> #dotnet #threadpool<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u043e \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u043e\u0435:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/737858\/\">Reciprocal throughput<\/a>\u00a0#microoptimization #low-level<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/740564\/\">\u0421\u043a\u0430\u0437\u043a\u0430 \u043f\u0440\u043e Branch prediction<\/a>\u00a0#microoptimization #low-level<\/p>\n<\/li>\n<\/ol>\n<p>\u0420\u0430\u0437\u043d\u043e\u0435:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/company\/skbkontur\/blog\/661097\/\">\u0421\u043a\u0430\u0437\u043a\u0430 \u043f\u0440\u043e Guid.NewGuid()<\/a>\u00a0#os_specific #dotnet #microoptimization<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/details>\n<p>\u0425\u043e\u0434\u044f\u0442 \u0441\u043b\u0443\u0445\u0438, \u0447\u0442\u043e foreach \u0431\u044b\u0441\u0442\u0440\u0435\u0435 for. \u0410 \u0435\u0449\u0451 \u0445\u043e\u0434\u044f\u0442 \u0441\u043b\u0443\u0445\u0438, \u0447\u0442\u043e for \u0431\u044b\u0441\u0442\u0440\u0435\u0435 foreach. \u041f\u043e\u0440\u0430 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435!<\/p>\n<details class=\"spoiler\">\n<summary>\u0425\u0443\u0434\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043e\u0442\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435, \u0448\u0443\u0442\u043a\u0430 \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0442\u0435\u043c\u0443. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u0437\u043d\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435, \u0447\u0438\u0442\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e.<\/summary>\n<div class=\"spoiler__content\">\n<p>\u2014 \u0421\u0430\u043d\u044f, \u0421\u0430\u043d\u044f, \u0430 \u0434\u0430\u0432\u0430\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043f\u043b\u0430\u0432\u0430\u0435\u0442? \u041c\u043e\u0439 \u0441\u0430\u043c\u043e\u043b\u0451\u0442 \u0438\u043b\u0438 \u0442\u0432\u043e\u0439 \u043f\u043e\u0435\u0437\u0434?<\/p>\n<p>\u2014 \u041e\u0433\u043e, \u043f\u043e\u0448\u043b\u0438 \u043d\u0430 \u0440\u0435\u0447\u043a\u0443, \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c!<\/p>\n<p>\u2026<\/p>\n<p>\u2014 \u0425\u0430, \u0442\u0432\u043e\u0439 \u043f\u043e\u0435\u0437\u0434 \u0443\u0442\u043e\u043d\u0443\u043b \u0440\u0430\u043d\u044c\u0448\u0435 \u043c\u043e\u0435\u0433\u043e \u0441\u0430\u043c\u043e\u043b\u0451\u0442\u0430!<\/p>\n<p>\u2014 \u041d\u0443 \u0438 \u043b\u0430\u0434\u043d\u043e. \u0410 \u0434\u0430\u0432\u0430\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u043b\u0435\u0442\u0430\u0435\u0442? \u0423\u0442\u044e\u0433 \u0438\u043b\u0438 \u043a\u0438\u0440\u043f\u0438\u0447?<\/p>\n<p>\u2014 \u0414\u0430\u0432\u0430\u0439, \u0441\u0435\u0439\u0447\u0430\u0441 \u044f \u0443\u0442\u044e\u0433 \u0438\u0437 \u0434\u043e\u043c\u0430 \u0432\u043e\u0437\u044c\u043c\u0443, \u0430 \u0442\u044b \u043a\u0438\u0440\u043f\u0438\u0447 \u0441\u043e \u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432\u043e\u0437\u044c\u043c\u0438<\/p>\n<p>\u2026<\/p>\n<p>\u2014 \u041a\u0438\u0434\u0430\u0435\u043c, \u0440\u0430\u0437, \u0434\u0432\u0430, \u0442\u0440\u0438!<\/p>\n<p>\u2014 \u041e\u0433\u043e, \u0443\u0442\u044e\u0433 \u0434\u0430\u043b\u044c\u0448\u0435 \u0443\u043b\u0435\u0442\u0435\u043b. \u042d\u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0443 \u043d\u0435\u0433\u043e \u0444\u043e\u0440\u043c\u0430 \u0430\u044d\u0440\u043e\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f!<\/p>\n<p>\u2014 \u0414\u0430\u0432\u0430\u0439 \u0435\u0449\u0451 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c. \u041a\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0448\u044c, \u0438\u0437 \u0447\u0435\u0433\u043e \u043b\u0443\u0447\u0448\u0435 \u043f\u0430\u0440\u0430\u0448\u044e\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f, \u0438\u0437 \u043f\u0440\u043e\u0441\u0442\u044b\u043d\u0438 \u0438\u043b\u0438 \u0441\u043a\u0430\u0442\u0435\u0440\u0442\u0438? \u041c\u043e\u0436\u043d\u043e \u0441 \u0447\u0435\u0440\u0434\u0430\u043a\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c.<\/p>\n<p>\u2026<\/p>\n<p>\u2014 \u042d\u0439, \u0431\u0435\u0437\u0434\u0430\u0440\u0438, \u0447\u0435\u0433\u043e \u0431\u0435\u0437 \u0434\u0435\u043b\u0430 \u0448\u043b\u044f\u0435\u0442\u0435\u0441\u044c? \u0410 \u043d\u0443 \u0443\u0440\u043e\u043a\u0438 \u0434\u0435\u043b\u0430\u0442\u044c!<\/p>\n<p>\u2014 \u041d\u0443 \u043c\u0430\u0430\u0430\u043c. \u041c\u044b \u0442\u0443\u0442 \u0432\u043e\u043e\u0431\u0449\u0435-\u0442\u043e \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u0441\u044f.<\/p>\n<p>\u2014 \u0423\u0440\u043e\u043a\u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u0448\u044c, \u043f\u043e\u0442\u043e\u043c \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u0448\u044c. \u041a\u0441\u0442\u0430\u0442\u0438, \u0442\u044b \u0443\u0442\u044e\u0433 \u043d\u0435 \u0442\u0440\u043e\u0433\u0430\u043b? \u041d\u0438\u043a\u0430\u043a \u043d\u0430\u0439\u0442\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443.<\/p>\n<\/div>\n<\/details>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u0430\u0438\u043c\u043e\u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c for \u0438 foreach. \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u044b. \u0422\u0430\u043a\u0436\u0435, \u044d\u0442\u043e \u0441\u043f\u0438\u0441\u043a\u0438 \u2014 List. \u0410 \u0435\u0449\u0451, \u0441\u044e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043d\u0435\u0441\u0442\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 \u2014 IList. \u041f\u043e\u0434 \u044d\u0442\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u0440\u044f\u0442\u0430\u0442\u044c \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0439 List, \u0442\u0430\u043a \u0438 \u043c\u0430\u0441\u0441\u0438\u0432 (\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c). \u0412\u0441\u044f\u043a\u0438\u0435 \u0438\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043b\u043e\u0432\u0430\u0440\u0438, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0442\u0440\u043e\u0438\u0442\u044c for \u043f\u043e \u043a\u043b\u044e\u0447\u0430\u043c-int&#8217;\u0430\u043c, \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c.<\/p>\n<p>\u0417\u0430\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u043d\u0430\u0448\u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"cs\">public class ForeachVsFor {     private List&lt;int> list; \/\/\u0421\u043f\u0438\u0441\u043e\u043a     private IList&lt;int> iListOverList; \/\/\u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList     private int[] array; \/\/\u041c\u0430\u0441\u0441\u0438\u0432     private IList&lt;int> iListOverArray; \/\/\u041c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList       [GlobalSetup]     public void Setup()     {         var length = 45;\/\/It's a good number. Not too small, not too big.         var ints = new List&lt;int>(length);         for (var i = 0; i &lt; length; i++)         {             ints.Add(i);         }         list = ints;         iListOverList = ints;         array = ints.ToArray();         iListOverArray = array;     } }<\/code><\/pre>\n<p>\u0414\u0430\u0431\u044b \u0438\u0441\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u044b\u0439 \u0446\u0438\u043a\u043b \u043d\u0435 \u0431\u044b\u043b \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0431\u0435\u0437 \u0442\u0435\u043b\u0430, \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u2014 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0411\u0443\u0434\u0435\u043c \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. \u0410 \u0435\u0449\u0451, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0435\u0440\u0447\u0438\u043d\u043a\u0438, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0438 \u043d\u0430 Net6, \u0438 \u043d\u0430 Net7!<\/p>\n<h3>\u041c\u0430\u0441\u0441\u0438\u0432\u044b<\/h3>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u043e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043a \u0441\u043b\u043e\u0436\u043d\u043e\u043c\u0443, \u0442\u043e \u0435\u0441\u0442\u044c \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432. \u041a\u0430\u043a \u0432\u044b \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForArray() {     var summ = 0;     for (int i = 0; i &lt; array.Length; i++)     {         summ += array[i];     }     return summ; }   [Benchmark] public int ForeachArray() {     var summ = 0;     foreach (var e in array)     {         summ += e;     }     return summ; }   [Benchmark] \/\/\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u0434\u0435\u0441\u044c \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043f\u043e\u043c\u043d\u0438\u0442 \u043f\u0440\u043e safety-check'\u0438 \/\/\u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/737858\/). public unsafe int ForUnsafeArray() {     var summ = 0;     var length = array.Length;     fixed (int* ptr = array)     {         var pointer = ptr;         var bound = pointer + length;         while (pointer != bound)         {             summ += *pointer;             pointer += 1;         }     }     return summ; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>|                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 6.0 |  25.52 ns |      - |         - | |          ForeachArray | .NET 6.0 |  15.87 ns |      - |         - | |        ForUnsafeArray | .NET 6.0 |  17.75 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 7.0 |  24.37 ns |      - |         - | |          ForeachArray | .NET 7.0 |  15.71 ns |      - |         - | |        ForUnsafeArray | .NET 7.0 |  18.22 ns |      - |         - |<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e foreach \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 for \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445, \u043f\u0440\u0438\u0447\u0435\u043c \u043d\u0430\u043c\u043d\u043e\u0433\u043e.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, ForUnsafe \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e For. \u0412 \u0447\u0435\u043c \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 For \u0438 ForUnsafe <a href=\"https:\/\/habr.com\/ru\/companies\/skbkontur\/articles\/737858\/\">\u043c\u044b \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u043c<\/a> \u2014 \u0432 \u043b\u0438\u0448\u043d\u0435\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u043d\u0430 \u043d\u0435\u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u043d\u0438\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0446\u0438\u0438.<\/p>\n<p>\u0418 ForUnsafe \u043b\u0438\u0448\u044c \u0447\u0443\u0442\u044c-\u0447\u0443\u0442\u044c \u0445\u0443\u0436\u0435 Foreach. \u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0437\u0443\u0447\u0430\u0442\u044c \u044d\u0442\u0443 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u043f\u043e\u0434 \u043c\u0438\u043a\u0440\u043e\u0441\u043a\u043e\u043f\u043e\u043c \u0432 ASM-\u043a\u043e\u0434\u0435.<\/p>\n<p>\u041c\u0435\u0436\u0434\u0443 Net6 \u0438 Net7 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f.<\/p>\n<\/div>\n<\/details>\n<h3>\u0421\u043f\u0438\u0441\u043a\u0438<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0441\u043f\u0438\u0441\u043a\u0430\u043c, \u0442\u043e \u0435\u0441\u0442\u044c List. \u0423 \u043d\u0438\u0445 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c, \u043a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0442\u043e\u0436\u0435 \u043c\u0430\u0441\u0441\u0438\u0432. \u041d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0438 \u0435\u0433\u043e, \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0443 \u043d\u0435\u0433\u043e \u0441\u0432\u043e\u0438. \u041a\u0430\u043a \u0432\u044b \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435:<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForList() {     var summ = 0;     for (int i = 0; i &lt; list.Count; i++)     {         summ += list[i];     }     return summ; }   [Benchmark] public int ForeachList() {     var summ = 0;     foreach (var e in list)     {         summ += e;     }     return summ; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0434\u043b\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>|                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |               ForList | .NET 6.0 |  32.96 ns |      - |         - | |           ForeachList | .NET 6.0 |  49.89 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |               ForList | .NET 7.0 |  32.25 ns |      - |         - | |           ForeachList | .NET 7.0 |  36.08 ns |      - |         - |   \u0421\u0442\u0430\u0440\u044b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0438 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434 \u0433\u043b\u0430\u0437\u0430\u043c\u0438 (\u0442\u043e\u043b\u044c\u043a\u043e Net7) |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 7.0 |  24.37 ns |      - |         - | |          ForeachArray | .NET 7.0 |  15.71 ns |      - |         - |<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 for \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 foreach, \u043f\u0440\u0438\u0447\u0435\u043c \u043d\u0430\u043c\u043d\u043e\u0433\u043e (\u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0431\u044b\u043b\u043e \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442!). \u041d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 Net6! \u0410 \u043d\u0430 Net7 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0443\u0436\u0435 \u043d\u0435 \u0442\u0430\u043a\u0430\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f, \u0445\u043e\u0442\u044f \u0438 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442. \u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0437\u0443\u0447\u0430\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0432 Net7 \u043b\u0443\u0447\u0448\u0435. \u041b\u0443\u0447\u0448\u0435 \u2014 \u0438 \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<p>\u041e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u043e\u0431\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u044e\u0442 \u043e\u0431\u043e\u0438\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u043c \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430.<\/p>\n<\/div>\n<\/details>\n<h3>\u041c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0438\u043f\u0430 IList, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u043b\u0438 \u043d\u0430\u0448 \u043c\u0430\u0441\u0441\u0438\u0432. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u043b\u0438 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u0430 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 IList.<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForIListOverArray() {     var summ = 0;     for (int i = 0; i &lt; iListOverArray.Count; i++)     {         summ += iListOverArray[i];     }     return summ; }   [Benchmark] public int ForeachIListOverArray() {     var summ = 0;     foreach (var e in iListOverArray)     {         summ += e;     }     return summ; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e\u0434 IList \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>|                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |     ForIListOverArray | .NET 6.0 | 210.78 ns |      - |         - | | ForeachIListOverArray | .NET 6.0 | 197.25 ns | 0.0076 |      32 B | |---------------------- |--------- |----------:|-------:|----------:| |     ForIListOverArray | .NET 7.0 | 220.61 ns |      - |         - | | ForeachIListOverArray | .NET 7.0 | 230.32 ns | 0.0076 |      32 B |   \u0421\u0442\u0430\u0440\u044b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0438, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434 \u0433\u043b\u0430\u0437\u0430\u043c\u0438 (\u0442\u043e\u043b\u044c\u043a\u043e Net7) |---------------------- |--------- |----------:|-------:|----------:| |               ForList | .NET 7.0 |  32.25 ns |      - |         - | |           ForeachList | .NET 7.0 |  36.08 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 7.0 |  24.37 ns |      - |         - | |          ForeachArray | .NET 7.0 |  15.71 ns |      - |         - |<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e foreach \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u0443\u0441\u043e\u0440\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438!<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0435, \u0447\u0442\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u2014 foreach \u043d\u0430 Net7 \u0441\u0442\u0430\u043b \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u043d\u0430 Net6!<\/p>\n<p>\u041d\u0443 \u0438 \u0442\u0440\u0435\u0442\u044c\u0435, \u0447\u0442\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u2014 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a IList&#8217;\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0445\u0443\u0436\u0435, \u0447\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u043a \u0441\u043f\u0438\u0441\u043a\u0443 \u0438\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0443!<\/p>\n<\/div>\n<\/details>\n<h3>\u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList<\/h3>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u0438\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c IList. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0438\u043f\u0430 IList, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u043b\u0438 \u043d\u0430\u0448 \u0441\u043f\u0438\u0441\u043e\u043a. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u043b\u0438 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c, \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u0430 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 IList.<\/p>\n<pre><code class=\"cs\">[Benchmark] public int ForIListOverList() {     var summ = 0;     for (int i = 0; i &lt; iListOverList.Count; i++)     {         summ += iListOverList[i];     }     return summ; }   [Benchmark] public int ForeachIListOverList() {     var summ = 0;     foreach (var e in iListOverList)     {         summ += e;     }     return summ; }<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u0434\u043b\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0434 IList \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>|                Method |  Runtime |      Mean |   Gen0 | Allocated | |---------------------- |--------- |----------:|-------:|----------:| |      ForIListOverList | .NET 6.0 | 190.77 ns |      - |         - | |  ForeachIListOverList | .NET 6.0 | 309.41 ns | 0.0095 |      40 B | |---------------------- |--------- |----------:|-------:|----------:| |      ForIListOverList | .NET 7.0 | 187.43 ns |      - |         - | |  ForeachIListOverList | .NET 7.0 | 355.90 ns | 0.0095 |      40 B |   \u0421\u0442\u0430\u0440\u044b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0438, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434 \u0433\u043b\u0430\u0437\u0430\u043c\u0438 (\u0442\u043e\u043b\u044c\u043a\u043e Net7) |---------------------- |--------- |----------:|-------:|----------:| |               ForList | .NET 7.0 |  32.25 ns |      - |         - | |           ForeachList | .NET 7.0 |  36.08 ns |      - |         - | |---------------------- |--------- |----------:|-------:|----------:| |              ForArray | .NET 7.0 |  24.37 ns |      - |         - | |          ForeachArray | .NET 7.0 |  15.71 ns |      - |         - |<\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-349398","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/349398","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=349398"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/349398\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=349398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=349398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=349398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}