title: find and findindex painful slow for List<Object> why?
body: <p>Im am working in a project that uses intensively List and i try to find the object via the name (that is a member of the object).</p>
  
  <p>My code worked without searching it using a single for-next loop (function find1) but i found that it is possible to the same using the build-in found find, and the code works. However, it feel a bit slow. So, i did a project for test the speed:</p>
  
  <p>I have the next code</p>
  
  <pre><code>    public List&lt;MyObject&gt; varbig = new List&lt;MyObject&gt;();
      public Dictionary&lt;string,string&gt; myDictionary=new Dictionary&lt;string, string&gt;();
      public Form1() {
          InitializeComponent();
      }
  
      private void button1_Click(object sender, EventArgs e) {
          myDictionary.Clear();
          varbig.Clear();
  
          for (int i = 0; i &lt; 5000; i++) {
              varbig.Add(new MyObject("name" + i.ToString(),"value"+i.ToString()));
              myDictionary.Add("name" + i.ToString(), i.ToString());
          }
          // first test
          var start1 = Environment.TickCount;
          for (int i = 0; i &lt; 3000; i++) {
              var ss=find1("name499");
          }
          var end1 = Environment.TickCount;
          Console.WriteLine("time  1 :" + (end1 - start1));
          // second test
          var start2 = Environment.TickCount;
          for (int i = 0; i &lt; 3000; i++) {
              var ss=find2("name499");
          }
          var end2 = Environment.TickCount;
          Console.WriteLine("time  2 :" + (end2 - start2));
          // third test
          var start3 = Environment.TickCount;
          for (int i = 0; i &lt; 3000; i++) {
              var ss = find3("name499");
          }
          var end3 = Environment.TickCount;
          Console.WriteLine("time  3 :" + (end3 - start3));
  
          // first test b
          var start1b = Environment.TickCount;
          for (int i = 0; i &lt; 3000; i++) {
              var ss=find1("name4999");
          }
          var end1b = Environment.TickCount;
          Console.WriteLine("timeb 1 :" + (end1b - start1b));
          // second test
          var start2b = Environment.TickCount;
          for (int i = 0; i &lt; 3000; i++) {
              var ss=find2("name4999");
          }
          var end2b = Environment.TickCount;
          Console.WriteLine("timeb 2 :" + (end2b - start2b));
          // third test
          var start3b = Environment.TickCount;
          for (int i = 0; i &lt; 3000; i++) {
              var ss = find3("name4999");
          }
          var end3b = Environment.TickCount;
          Console.WriteLine("timeb 3 :" + (end3b - start3b));
  
      }
  
      public int find1(string name) {
          for (int i = 0; i &lt; varbig.Count; i++) {
              if(varbig[i].Name == name) {
                  return i;
              }
          }
          return -1;
      }
  
      public int find2(string name) {
          int idx = varbig.FindIndex(tmpvar =&gt; Name == name);
          return idx;
      }
      public int find3(string name) {
          var ss=myDictionary[name];
          return int.Parse(ss);
      }
  }
  </code></pre>
  
  <p>And i use the next object</p>
  
  <pre><code>public class MyObject {
      private string _name = "";
      private string _value = "";
      public MyObject() {}
  
      public MyObject(string name, string value) {
          _name = name;
          _value = value;
      }
  
      public string Name {
          get { return _name; }
          set { _name = value; }
      }
  
      public string Value {
          get { return _value; }
          set { _value = value; }
      }
  }
  </code></pre>
  
  <p>Mostly it do the next thing:
  I create an array with 5000 elements.</p>
  
  <p>time 1 = search the 499th object (index) using a simple for-next.</p>
  
  <p>time 2 = search the 499th using the build in function find of List</p>
  
  <p>time 3 = it do the search of the 499th element using dictionary.</p>
  
  <p>Timeb 1, timeb 2 and timeb 3 do the same but try to search the 4999th element instead of the 499th element.</p>
  
  <p>I ran a couple of times :</p>
  
  <ul>
  <li>time  1 :141</li>
  <li>time  2 :1248</li>
  <li>time  3 :0</li>
  <li>timeb 1 :811</li>
  <li>timeb 2 :1170</li>
  <li><p>timeb 3 :0</p></li>
  <li><p>time  1 :109</p></li>
  <li>time  2 :1170</li>
  <li>time  3 :0</li>
  <li>timeb 1 :796</li>
  <li>timeb 2 :1170</li>
  <li>timeb 3 :0</li>
  </ul>
  
  <p><em>(the small then the fast)</em></p>
  
  <p>And, for my surprise, the build in function <strong>findindex</strong> is absurdly slow (in some cases, close to 10x slower. Also, the dictionary approach is almost instantly.</p>
  
  <p>My question is, why?. is it because the predicate?.  </p>