Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandrnikitin/b50276137d6529650165 to your computer and use it in GitHub Desktop.
Save alexandrnikitin/b50276137d6529650165 to your computer and use it in GitHub Desktop.
PerformanceProblemWithNestedClass_With_T()
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