Skip to content

Instantly share code, notes, and snippets.

@alexandrnikitin
Last active August 29, 2015 14:10

Revisions

  1. alexandrnikitin renamed this gist Feb 4, 2015. 1 changed file with 0 additions and 0 deletions.
  2. alexandrnikitin revised this gist Nov 30, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions PerformanceProblemWithNestedClass
    Original file line number Diff line number Diff line change
    @@ -43,6 +43,10 @@ namespace PerformanceProblem
    for (var i = 0; i < 8000000; i++)
    {
    if (_list.Any())
    // or if (_list.Count() > 0)
    // or if (_list.FirstOrDefault() != null)
    // or if (_list.SingleOrDefault() != null)
    // or other IEnumerable<T> method
    {
    return;
    }
  3. alexandrnikitin revised this gist Nov 28, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion PerformanceProblemWithNestedClass
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,10 @@ namespace PerformanceProblem
    {
    for (var i = 0; i < 8000000; i++)
    {
    _list.Any();
    if (_list.Any())
    {
    return;
    }
    }
    }
    }
  4. alexandrnikitin revised this gist Nov 27, 2014. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions PerformanceProblemWithNestedClass
    Original file line number Diff line number Diff line change
    @@ -9,30 +9,33 @@ namespace PerformanceProblem
    {
    public static void Main()
    {
    Measure(new StringClass());
    Measure(new ClassBase<string>());
    Measure(new DerivedClass());
    Measure(new BaseClass<object>());
    }

    private static void Measure(ClassBase<string> classBase)
    private static void Measure(BaseClass<object> baseClass)
    {
    var sw = Stopwatch.StartNew();
    classBase.Run();
    baseClass.Run();
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
    }
    }

    public class StringClass : ClassBase<string>
    public class DerivedClass : BaseClass<object>
    {
    }

    public class ClassBase<T>
    public class BaseClass<T>
    {
    private List<T> _list = new List<T>();

    public ClassBase()
    public BaseClass()
    {
    Enumerable.Empty<T>();
    // or Enumerable.Repeat(new T(), 10);
    // or even new T();
    // or foreach (var item in _list) {}
    }

    public void Run()
  5. alexandrnikitin revised this gist Nov 27, 2014. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions PerformanceProblemWithNestedClass
    Original file line number Diff line number Diff line change
    @@ -39,10 +39,7 @@ namespace PerformanceProblem
    {
    for (var i = 0; i < 8000000; i++)
    {
    if (_list.Any())
    {
    return;
    }
    _list.Any();
    }
    }
    }
  6. alexandrnikitin revised this gist Nov 27, 2014. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions PerformanceProblemWithNestedClass
    Original file line number Diff line number Diff line change
    @@ -22,10 +22,6 @@ namespace PerformanceProblem
    }
    }

    public class Node<T>
    {
    }

    public class StringClass : ClassBase<string>
    {
    }
    @@ -36,7 +32,7 @@ namespace PerformanceProblem

    public ClassBase()
    {
    Enumerable.Empty<Node<T>>();
    Enumerable.Empty<T>();
    }

    public void Run()
  7. alexandrnikitin revised this gist Nov 27, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion PerformanceProblemWithNestedClass
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,6 @@ namespace PerformanceProblem
    public class ClassBase<T>
    {
    private List<T> _list = new List<T>();
    private Node<T> _node = new Node<T>();

    public ClassBase()
    {
  8. alexandrnikitin revised this gist Nov 27, 2014. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions PerformanceProblemWithNestedClass
    Original file line number Diff line number Diff line change
    @@ -37,9 +37,7 @@ namespace PerformanceProblem

    public ClassBase()
    {
    foreach (var node in Enumerable.Empty<Node<T>>())
    {
    }
    Enumerable.Empty<Node<T>>();
    }

    public void Run()
  9. alexandrnikitin revised this gist Nov 27, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions PerformanceProblemWithNestedClass
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,6 @@ namespace PerformanceProblem

    public class Node<T>
    {
    public List<T> List = new List<T>();
    }

    public class StringClass : ClassBase<string>
    @@ -33,7 +32,8 @@ namespace PerformanceProblem

    public class ClassBase<T>
    {
    public Node<T> Node = new Node<T>();
    private List<T> _list = new List<T>();
    private Node<T> _node = new Node<T>();

    public ClassBase()
    {
    @@ -46,7 +46,7 @@ namespace PerformanceProblem
    {
    for (var i = 0; i < 8000000; i++)
    {
    if (Node.List.Any())
    if (_list.Any())
    {
    return;
    }
  10. alexandrnikitin created this gist Nov 27, 2014.
    56 changes: 56 additions & 0 deletions PerformanceProblemWithNestedClass
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;

    namespace PerformanceProblem
    {
    public class Program
    {
    public static void Main()
    {
    Measure(new StringClass());
    Measure(new ClassBase<string>());
    }

    private static void Measure(ClassBase<string> classBase)
    {
    var sw = Stopwatch.StartNew();
    classBase.Run();
    sw.Stop();
    Console.WriteLine(sw.ElapsedMilliseconds);
    }
    }

    public class Node<T>
    {
    public List<T> List = new List<T>();
    }

    public class StringClass : ClassBase<string>
    {
    }

    public class ClassBase<T>
    {
    public Node<T> Node = new Node<T>();

    public ClassBase()
    {
    foreach (var node in Enumerable.Empty<Node<T>>())
    {
    }
    }

    public void Run()
    {
    for (var i = 0; i < 8000000; i++)
    {
    if (Node.List.Any())
    {
    return;
    }
    }
    }
    }
    }