Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 5.59 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Linq performance and delayed execution
  2. namespace ForEachForLINQPerTest
  3. {
  4.  class IntBox
  5.  {
  6.     public int fieldX;
  7.     public int PropertyX { get; set; }
  8.  }
  9.  
  10.  public partial class MainPage : PhoneApplicationPage
  11.  {
  12.     /// <summary>
  13.     /// size of tested List
  14.     /// </summary>
  15.     public const int TEST_SIZE = 1000000;
  16.  
  17.     //
  18.     private List<int> m_intList = new List<int>(TEST_SIZE);
  19.  
  20.     //
  21.     private List<IntBox> m_intBoxList = new List<IntBox>(TEST_SIZE);
  22.  
  23.     //
  24.     private Stopwatch m_stopwatch = null;
  25.     // Constructor
  26.     public MainPage()
  27.     {
  28.         InitializeComponent();
  29.  
  30.         for (int i = 0; i < TEST_SIZE; ++i)
  31.         {
  32.             m_intBoxList.Add( new IntBox());
  33.             m_intList.Add(0);
  34.         }
  35.     }
  36.  
  37.     private void startButton_Click(object sender, RoutedEventArgs e)
  38.     {
  39.         var forTest = ForTest(); // Jitter preheat
  40.         forTest = ForTest();
  41.         forResultTextBlock.Text = forTest;
  42.  
  43.         var foreachTest = ForEachTest();
  44.         foreachTest = ForEachTest();
  45.         foreachResultTextBlock.Text = foreachTest;
  46.  
  47.         var exTest = Extenstion();
  48.         exTest = Extenstion();
  49.         ExtensionResultTextBlock.Text = exTest;
  50.  
  51.         var linqTest = LINQTest();
  52.         linqTest = LINQTest();
  53.         LINQResultTextBlock.Text = linqTest;
  54.     }
  55.  
  56.     private string LINQTest()
  57.     {
  58.         m_stopwatch = new Stopwatch();
  59.         m_stopwatch.Start();
  60.         long temp = 0;
  61.         var result = from x in m_intList
  62.                      select temp += x;            
  63.         m_stopwatch.Stop();
  64.         var intListTime = m_stopwatch.ElapsedMilliseconds;
  65.         m_stopwatch.Reset();
  66.         result.ToList();
  67.  
  68.  
  69.         m_stopwatch.Start();
  70.         var result2 = from x in m_intBoxList
  71.                      select temp += x.fieldX;    
  72.         m_stopwatch.Stop();
  73.         var intBoxListFieldTime = m_stopwatch.ElapsedMilliseconds;
  74.         m_stopwatch.Reset();
  75.         result2.ToList();
  76.  
  77.         m_stopwatch.Start();
  78.         var result3 = from x in m_intBoxList
  79.                       select temp += x.PropertyX;    
  80.         m_stopwatch.Stop();
  81.         var intBoxListPropertyTime = m_stopwatch.ElapsedMilliseconds;
  82.         m_stopwatch.Reset();
  83.         result3.ToList();
  84.         return String.Format("LINQ test List<int> = {0} n List<IntBox> field = {1} n List<IntBos> property = {2}", intListTime, intBoxListFieldTime, intBoxListPropertyTime);
  85.     }
  86.  
  87.     private string Extenstion()
  88.     {
  89.         m_stopwatch = new Stopwatch();
  90.         m_stopwatch.Start();
  91.         long temp = 0;
  92.         m_intList.ForEach(i => temp += i);
  93.         m_stopwatch.Stop();
  94.         var intListTime = m_stopwatch.ElapsedMilliseconds;
  95.         m_stopwatch.Reset();
  96.  
  97.  
  98.         m_stopwatch.Start();
  99.         m_intBoxList.ForEach(i => temp += i.fieldX);
  100.         m_stopwatch.Stop();
  101.         var intBoxListFieldTime = m_stopwatch.ElapsedMilliseconds;
  102.         m_stopwatch.Reset();
  103.  
  104.         m_stopwatch.Start();
  105.         m_intBoxList.ForEach(i => temp += i.PropertyX);
  106.         m_stopwatch.Stop();
  107.         var intBoxListPropertyTime = m_stopwatch.ElapsedMilliseconds;
  108.         m_stopwatch.Reset();
  109.         return String.Format("Extenstion test List<int> = {0} n List<IntBox> field = {1} n List<IntBos> property = {2}", intListTime, intBoxListFieldTime, intBoxListPropertyTime);
  110.     }
  111.  
  112.     private string ForEachTest()
  113.     {
  114.         m_stopwatch = new Stopwatch();
  115.         long temp = 0;
  116.         m_stopwatch.Start();
  117.         foreach(int item in m_intList)
  118.         {
  119.            temp += item;
  120.         }
  121.         m_stopwatch.Stop();
  122.         var intListTime = m_stopwatch.ElapsedMilliseconds;
  123.         m_stopwatch.Reset();
  124.  
  125.  
  126.         m_stopwatch.Start();
  127.         foreach (IntBox item in m_intBoxList)
  128.         {
  129.             temp += item.fieldX;
  130.         }
  131.         m_stopwatch.Stop();
  132.         var intBoxListFieldTime = m_stopwatch.ElapsedMilliseconds;
  133.         m_stopwatch.Reset();
  134.  
  135.         m_stopwatch.Start();
  136.         foreach (IntBox item in m_intBoxList)
  137.         {
  138.             temp += item.PropertyX;
  139.         }
  140.         m_stopwatch.Stop();
  141.         var intBoxListPropertyTime = m_stopwatch.ElapsedMilliseconds;
  142.         m_stopwatch.Reset();
  143.  
  144.         return String.Format("ForEach test List<int> = {0} n List<IntBox> field = {1} n List<IntBos> property = {2}", intListTime, intBoxListFieldTime, intBoxListPropertyTime);
  145.     }
  146.  
  147.     private string ForTest()
  148.     {
  149.         m_stopwatch = new Stopwatch();
  150.         m_stopwatch.Start();
  151.         long temp = 0;
  152.         for (int i = 0; i < TEST_SIZE; ++i)
  153.         {
  154.             temp += m_intList[i];
  155.         }
  156.         m_stopwatch.Stop();
  157.         var intListTime = m_stopwatch.ElapsedMilliseconds;
  158.         m_stopwatch.Reset();
  159.  
  160.  
  161.         m_stopwatch.Start();
  162.         for (int i = 0; i < m_intList.Count; ++i)
  163.         {
  164.             temp += m_intBoxList[i].fieldX;
  165.         }
  166.         m_stopwatch.Stop();
  167.         var intBoxListFieldTime = m_stopwatch.ElapsedMilliseconds;
  168.         m_stopwatch.Reset();
  169.  
  170.         m_stopwatch.Start();
  171.         for (int i = 0; i < m_intList.Count; ++i)
  172.         {
  173.            temp +=  m_intBoxList[i].PropertyX;
  174.         }
  175.         m_stopwatch.Stop();
  176.         var intBoxListPropertyTime = m_stopwatch.ElapsedMilliseconds;
  177.         m_stopwatch.Reset();
  178.  
  179.         return String.Format("For loop test List<int> = {0} n List<IntBox> field = {1} n List<IntBos> property = {2}", intListTime, intBoxListFieldTime, intBoxListPropertyTime);
  180.     }
  181.  }
  182. }
  183.        
  184. m_stopwatch = new Stopwatch();
  185.         m_stopwatch.Start();
  186.         long temp = 0;
  187.         var result = from x in m_intList
  188.                      select temp += x;            
  189.         m_stopwatch.Stop();
  190.         var intListTime = m_stopwatch.ElapsedMilliseconds;
  191.         m_stopwatch.Reset();
  192.         result.ToList();