{"id":1507,"date":"2016-05-18T12:48:50","date_gmt":"2016-05-18T04:48:50","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1507"},"modified":"2016-05-18T12:48:50","modified_gmt":"2016-05-18T04:48:50","slug":"%e6%b8%ac%e8%a9%a6-linq-first-%e7%9a%84%e9%80%9f%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/1507\/","title":{"rendered":"\u6e2c\u8a66 Linq First \u7684\u901f\u5ea6"},"content":{"rendered":"<p>\u6e2c\u8a66\u4e00\u4e0b Linq \u7684 First \u901f\u5ea6, \u4ee5\u4e0b\u662f 1000000 \u6b21\u7684\u7d50\u679c.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1508\" src=\"http:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2016\/05\/LinqTest.png\" alt=\"LinqTest\" width=\"725\" height=\"240\" srcset=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2016\/05\/LinqTest.png 725w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2016\/05\/LinqTest-300x99.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><\/p>\n<pre class=\"brush:csharp\">using UnityEngine;\r\nusing System.Linq;\r\nusing System.Collections.Generic;\r\nusing System.Diagnostics;\r\n\r\npublic class testListFirst : MonoBehaviour\r\n{\r\n\tpublic int limit = 1000;\r\n\r\n\t[System.Serializable]\r\n\tpublic struct MyStruct\r\n\t{\r\n\t\tpublic string str;\r\n\t\tpublic int num;\r\n\t}\r\n\r\n\tList&lt;MyStruct&gt; m_list = new List&lt;MyStruct&gt;();\r\n\r\n\r\n\tvoid Awake()\r\n\t{\r\n\t\tm_list.Add(new MyStruct()\r\n\t\t{\r\n\t\t\tstr = \"str\" + Random.Range(0, 100000),\r\n\t\t\tnum = 1\r\n\t\t});\r\n\t}\r\n\r\n\t\/\/ Use this for initialization\r\n\tvoid OnEnable ()\r\n\t{\r\n\t\tint mem = 0;\r\n\t\t\r\n\t\tStopwatch clock = new Stopwatch();\r\n\r\n\t\t\/\/ Case C\r\n\t\tmem = 0;\r\n\t\tclock.Reset();\r\n\t\tclock.Start();\r\n\t\tfor (int i = 0; i &lt; limit; i++)\r\n\t\t{\r\n\t\t\tmem += m_list.FirstOrDefault().num;\r\n\t\t}\r\n\r\n\t\tclock.Stop();\r\n\t\tUnityEngine.Debug.Log(\"list.FirstOrDefault Time used : \" + clock.ElapsedMilliseconds + \"ms Avg. \" + (clock.ElapsedMilliseconds \/ limit));\r\n\r\n\t\t\/\/ Case A\r\n\t\tmem = 0;\r\n\t\tclock.Reset();\r\n\t\tclock.Start();\r\n\t\tfor(int i=0; i&lt; limit; i++)\r\n\t\t{\r\n\t\t\tmem += m_list[0].num;\r\n\t\t}\r\n\t\tclock.Stop();\r\n\t\tUnityEngine.Debug.Log(\"list[0] Time used : \" + clock.ElapsedMilliseconds + \"ms Avg. \" + (clock.ElapsedMilliseconds \/ limit));\r\n\r\n\t\tclock.Reset();\r\n\t\tmem = 0;\r\n\r\n\t\t\/\/ Case B\r\n\t\tmem = 0;\r\n\t\tclock.Reset();\r\n\t\tclock.Start();\r\n\t\tfor (int i = 0; i &lt; limit; i++)\r\n\t\t{\r\n\t\t\tmem += m_list.First().num;\r\n\t\t}\r\n\r\n\t\tclock.Stop();\r\n\t\tUnityEngine.Debug.Log(\"list.First Time used : \" + clock.ElapsedMilliseconds + \"ms Avg. \" + (clock.ElapsedMilliseconds \/ limit));\r\n\r\n\r\n\t\t\/\/ Case D\r\n\t\tmem = 0;\r\n\t\tclock.Reset();\r\n\t\tclock.Start();\r\n\t\tfor (int i = 0; i &lt; limit; i++)\r\n\t\t{\r\n\t\t\tif(m_list.Count &gt; 0)\r\n\t\t\t\tmem += m_list.First().num;\r\n\t\t}\r\n\r\n\t\tclock.Stop();\r\n\t\tUnityEngine.Debug.Log(\"Count first + list.First Time used : \" + clock.ElapsedMilliseconds + \"ms Avg. \" + (clock.ElapsedMilliseconds \/ limit));\r\n\r\n\t\t\/\/ Case E\r\n\t\tmem = 0;\r\n\t\tclock.Reset();\r\n\t\tclock.Start();\r\n\t\tfor (int i = 0; i &lt; limit; i++)\r\n\t\t{\r\n\t\t\tif (m_list.Count() &gt; 0)\r\n\t\t\t\tmem += m_list.First().num;\r\n\t\t}\r\n\r\n\t\tclock.Stop();\r\n\t\tUnityEngine.Debug.Log(\"Count() first + list.First Time used : \" + clock.ElapsedMilliseconds + \"ms Avg. \" + (clock.ElapsedMilliseconds \/ limit));\r\n\t}\r\n\t\r\n\t\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6e2c\u8a66\u4e00\u4e0b Linq \u7684 First \u901f\u5ea6, \u4ee5\u4e0b\u662f 1000000 \u6b21\u7684\u7d50\u679c. using Unit &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11,4],"tags":[16,68],"class_list":["post-1507","post","type-post","status-publish","format-standard","hentry","category-c","category-unity3d","category-program","tag-c-2","tag-linq"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1507"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1507\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}