- Linq performance and delayed execution
- namespace ForEachForLINQPerTest
- {
- class IntBox
- {
- public int fieldX;
- public int PropertyX { get; set; }
- }
- public partial class MainPage : PhoneApplicationPage
- {
- /// <summary>
- /// size of tested List
- /// </summary>
- public const int TEST_SIZE = 1000000;
- //
- private List<int> m_intList = new List<int>(TEST_SIZE);
- //
- private List<IntBox> m_intBoxList = new List<IntBox>(TEST_SIZE);
- //
- private Stopwatch m_stopwatch = null;
- // Constructor
- public MainPage()
- {
- InitializeComponent();
- for (int i = 0; i < TEST_SIZE; ++i)
- {
- m_intBoxList.Add( new IntBox());
- m_intList.Add(0);
- }
- }
- private void startButton_Click(object sender, RoutedEventArgs e)
- {
- var forTest = ForTest(); // Jitter preheat
- forTest = ForTest();
- forResultTextBlock.Text = forTest;
- var foreachTest = ForEachTest();
- foreachTest = ForEachTest();
- foreachResultTextBlock.Text = foreachTest;
- var exTest = Extenstion();
- exTest = Extenstion();
- ExtensionResultTextBlock.Text = exTest;
- var linqTest = LINQTest();
- linqTest = LINQTest();
- LINQResultTextBlock.Text = linqTest;
- }
- private string LINQTest()
- {
- m_stopwatch = new Stopwatch();
- m_stopwatch.Start();
- long temp = 0;
- var result = from x in m_intList
- select temp += x;
- m_stopwatch.Stop();
- var intListTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- result.ToList();
- m_stopwatch.Start();
- var result2 = from x in m_intBoxList
- select temp += x.fieldX;
- m_stopwatch.Stop();
- var intBoxListFieldTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- result2.ToList();
- m_stopwatch.Start();
- var result3 = from x in m_intBoxList
- select temp += x.PropertyX;
- m_stopwatch.Stop();
- var intBoxListPropertyTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- result3.ToList();
- return String.Format("LINQ test List<int> = {0} n List<IntBox> field = {1} n List<IntBos> property = {2}", intListTime, intBoxListFieldTime, intBoxListPropertyTime);
- }
- private string Extenstion()
- {
- m_stopwatch = new Stopwatch();
- m_stopwatch.Start();
- long temp = 0;
- m_intList.ForEach(i => temp += i);
- m_stopwatch.Stop();
- var intListTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- m_stopwatch.Start();
- m_intBoxList.ForEach(i => temp += i.fieldX);
- m_stopwatch.Stop();
- var intBoxListFieldTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- m_stopwatch.Start();
- m_intBoxList.ForEach(i => temp += i.PropertyX);
- m_stopwatch.Stop();
- var intBoxListPropertyTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- return String.Format("Extenstion test List<int> = {0} n List<IntBox> field = {1} n List<IntBos> property = {2}", intListTime, intBoxListFieldTime, intBoxListPropertyTime);
- }
- private string ForEachTest()
- {
- m_stopwatch = new Stopwatch();
- long temp = 0;
- m_stopwatch.Start();
- foreach(int item in m_intList)
- {
- temp += item;
- }
- m_stopwatch.Stop();
- var intListTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- m_stopwatch.Start();
- foreach (IntBox item in m_intBoxList)
- {
- temp += item.fieldX;
- }
- m_stopwatch.Stop();
- var intBoxListFieldTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- m_stopwatch.Start();
- foreach (IntBox item in m_intBoxList)
- {
- temp += item.PropertyX;
- }
- m_stopwatch.Stop();
- var intBoxListPropertyTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- return String.Format("ForEach test List<int> = {0} n List<IntBox> field = {1} n List<IntBos> property = {2}", intListTime, intBoxListFieldTime, intBoxListPropertyTime);
- }
- private string ForTest()
- {
- m_stopwatch = new Stopwatch();
- m_stopwatch.Start();
- long temp = 0;
- for (int i = 0; i < TEST_SIZE; ++i)
- {
- temp += m_intList[i];
- }
- m_stopwatch.Stop();
- var intListTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- m_stopwatch.Start();
- for (int i = 0; i < m_intList.Count; ++i)
- {
- temp += m_intBoxList[i].fieldX;
- }
- m_stopwatch.Stop();
- var intBoxListFieldTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- m_stopwatch.Start();
- for (int i = 0; i < m_intList.Count; ++i)
- {
- temp += m_intBoxList[i].PropertyX;
- }
- m_stopwatch.Stop();
- var intBoxListPropertyTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- return String.Format("For loop test List<int> = {0} n List<IntBox> field = {1} n List<IntBos> property = {2}", intListTime, intBoxListFieldTime, intBoxListPropertyTime);
- }
- }
- }
- m_stopwatch = new Stopwatch();
- m_stopwatch.Start();
- long temp = 0;
- var result = from x in m_intList
- select temp += x;
- m_stopwatch.Stop();
- var intListTime = m_stopwatch.ElapsedMilliseconds;
- m_stopwatch.Reset();
- result.ToList();