Last active
August 29, 2015 14:10
-
-
Save alexandrnikitin/b50276137d6529650165 to your computer and use it in GitHub Desktop.
PerformanceProblemWithNestedClass_With_T()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
namespace PerformanceProblem | |
{ | |
public class Program | |
{ | |
public static void Main() | |
{ | |
Measure(new DerivedClass()); | |
Measure(new BaseClass<object>()); | |
} | |
private static void Measure(BaseClass<object> baseClass) | |
{ | |
var sw = Stopwatch.StartNew(); | |
baseClass.Run(); | |
sw.Stop(); | |
Console.WriteLine(sw.ElapsedMilliseconds); | |
} | |
} | |
public class DerivedClass : BaseClass<object> | |
{ | |
} | |
public class BaseClass<T> | |
where T : new() | |
{ | |
private List<T> _list = new List<T>(); | |
public BaseClass() | |
{ | |
//Enumerable.Empty<T>(); | |
// or Enumerable.Repeat(new T(), 10); | |
new T(); | |
// or foreach (var item in _list) {} | |
} | |
public void Run() | |
{ | |
for (var i = 0; i < 8000000; i++) | |
{ | |
if (_list.Any()) | |
{ | |
return; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment