Advertisement
NPSF3000

List Dot For MicroBenchmark

Nov 26th, 2011
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. /*
  2.  * WARNING THIS IS A MICRO BENCHMARK!
  3.  *
  4.  * A responce to http://forum.unity3d.com/threads/90719-C-having-trouble-using-.find-on-a-List
  5.  *
  6.  * A NPSF3000 test.
  7.  *
  8.  * Conclusion - use what you want, don't microoptimise for the for version.  Not only is it harder to read [for lamba fluet programmers] the performance in a 'typical' implementation is appalling.  
  9.  
  10. 27/11/11 - Q6600, Win7 64, .net 4.0
  11.  
  12. Rough Results ('000 Its/Second)                
  13.  
  14. Iterations   List.For   For    
  15. 1           180         100    
  16. 100         750         120-180    
  17. 10000       1000        120-180    
  18. 1000000     1000        180     [Very consistant]
  19.  *
  20.  *
  21. [I did a quick test and cached the data.Count() in the For version.  It boosted the 1Mn it result from 180 to 17000.  So this doesn't prove that List.For is faster than For, but it does restate the need to either spend the LOC on performance [in this particular instance a waste], or go with the built in option.  Going with for because it is faster without testing and optimisation can lead to poorer perfomance and maintanability!.]
  22.  *
  23.  */
  24.  
  25.  
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Text;
  30. using System.Diagnostics;
  31.  
  32.  
  33.  
  34. namespace List_Dot_Find
  35. {
  36.     class Program
  37.     {
  38.         static void Main(string[] args)
  39.         {
  40.             const int iterations = 1000000;
  41.  
  42.             var data = new List<MyClass>(200);
  43.  
  44.             for (var i = 0; i < 200; i++)
  45.                 data.Add(new MyClass(i, "Guest " + i));
  46.  
  47.             var rnd = new Random();
  48.  
  49.             while (true)
  50.             {
  51.                 var sw1 = Stopwatch.StartNew();
  52.  
  53.                 for (var i = 0; i < iterations; i++)
  54.                 {
  55.                     var findID = rnd.Next(200);
  56.                     data.Find(x => x.ID == findID);
  57.                 }
  58.  
  59.                 sw1.Stop();
  60.  
  61.                 var sw2 = Stopwatch.StartNew();
  62.  
  63.                 for (var i = 0; i < iterations; i++)
  64.                 {
  65.                     var findID = rnd.Next(200);
  66.  
  67.                     for (var x = 0; x < data.Count(); x++)
  68.                         if (data[x].ID == findID) break;
  69.                 }
  70.  
  71.                 sw2.Stop();
  72.  
  73.                 Console.WriteLine();
  74.                 Console.WriteLine("{0} iterations:", iterations);
  75.                 Console.WriteLine("List.Find: {0}/s", (iterations * Stopwatch.Frequency / sw1.ElapsedTicks ).ToString("N0"));
  76.                 Console.WriteLine("For      : {0}/s", (iterations * Stopwatch.Frequency / sw2.ElapsedTicks).ToString("N0"));
  77.                 Console.WriteLine();
  78.                 Console.WriteLine();
  79.                 Console.ReadLine();
  80.             }
  81.         }
  82.     }
  83.  
  84.     public class MyClass
  85.     {
  86.  
  87.         public int ID
  88.         {
  89.             get { return _id; }
  90.         }
  91.         public string Name
  92.         {
  93.             get { return _name; }
  94.         }
  95.  
  96.         private int _id;
  97.         private string _name = string.Empty;
  98.  
  99.         // constructor
  100.         public MyClass(int id, string name)
  101.         {
  102.             try
  103.             {
  104.                 _id = ID;
  105.                 _name = name;
  106.             }
  107.             catch (Exception e)
  108.             {
  109.                 Console.WriteLine("Error in MyClass Constructor1:" + e);
  110.             }
  111.         }
  112.     }
  113. }
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement