CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

C# Loop Performance Test (correction)

866 Views
Copy Code Show/Hide Line Numbers
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Diagnostics;
   6:   
   7:  namespace Nixoncorp.LoopPerformance
   8:  {
   9:      class Test
  10:      {
  11:          delegate void TestMethod();
  12:          List<int> m_Source= new List<int>();
  13:          System.Diagnostics.Stopwatch m_Watch 
  14:                  = new System.Diagnostics.Stopwatch();
  15:   
  16:          public void AllTests()
  17:          {
  18:              for (int i = 0; i < 10000000; i++)
  19:                  m_Source.Add(i);
  20:              TestMethod _TestFor = new TestMethod(TestFor);
  21:              TestMethod _TestForEach = new TestMethod(TestForEach);
  22:              TestMethod _TestForEachDelegate = new TestMethod(TestForEachDelegate);
  23:              TestMethod _TestLinq = new TestMethod(TestLinq);
  24:              TestMethod _TestLambda = new TestMethod(TestLambda);
  25:              TestMethod _TestParallel = new TestMethod(TestParallel);
  26:   
  27:              // how many time to repeat each test?
  28:   
  29:              for (int i = 1; i < 17; i++)
  30:              {
  31:                  TestThis(_TestFor);
  32:                  TestThis(_TestForEach);
  33:                  TestThis(_TestForEachDelegate);
  34:                  TestThis(_TestLinq);
  35:                  TestThis(_TestLambda);
  36:                  TestThis(_TestParallel);
  37:                  Console.WriteLine(string.Empty);
  38:              }
  39:   
  40:              Console.Read();
  41:          }
  42:   
  43:          void TestThis(TestMethod test)
  44:          {
  45:              m_Watch.Reset();
  46:              m_Watch.Start();
  47:              test.Invoke();
  48:              var _Time = (int)m_Watch.Elapsed.TotalMilliseconds;
  49:              var _Method = test.Method.Name.Replace("Test", string.Empty);
  50:              var _Result = string.Format("{1}\t{0}", _Method, _Time);
  51:              Console.WriteLine(_Result);
  52:          }
  53:   
  54:          void TestFor()
  55:          {
  56:              var _Target = new List<int>();
  57:              for (int i = 0; i < m_Source.Count(); i++)
  58:                  _Target.Add(m_Source[i] + 1);
  59:              Debug.Assert(m_Source.Count() == _Target.ToArray().Count());
  60:          }
  61:   
  62:          void TestForEach()
  63:          {
  64:              var _Target = new List<int>();
  65:              foreach (int i in m_Source)
  66:                  _Target.Add(i + 1);
  67:              Debug.Assert(m_Source.Count() == _Target.ToArray().Count());
  68:          }
  69:   
  70:          void TestForEachDelegate()
  71:          {
  72:              var _Target = new List<int>();
  73:              m_Source.ForEach((int i) =>
  74:              {
  75:                  _Target.Add(i + 1);
  76:              });
  77:              Debug.Assert(m_Source.Count() == _Target.ToArray().Count());
  78:          }
  79:   
  80:          void TestLinq()
  81:          {
  82:              var _Target = from i in m_Source
  83:                            select i + 1;
  84:              Debug.Assert(m_Source.Count() == _Target.ToArray().Count());
  85:          }
  86:   
  87:          void TestLambda()
  88:          {
  89:              var _Target = m_Source.Select(i => i + 1);
  90:              Debug.Assert(m_Source.Count() == _Target.ToArray().Count());
  91:          }
  92:   
  93:          void TestParallel()
  94:          {
  95:              // TODO
  96:          }
  97:      }
  98:  }
by Jerry Nixon
  February 23, 2010 @ 12:54pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate