Код: Выделить всё
namespace InterfaceCall
{
using System;
using System.Diagnostics;
internal class Program
{
static void Main(string[] args)
{
while (true)
{
Test concreteTest = new Test();
Call(concreteTest);
ITest interfaceTest = new Test();
Call(interfaceTest);
Console.ReadLine();
}
}
private static void Call(Test test)
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++)
{
test.Increment();
}
sw.Stop();
Console.WriteLine($"Concrete time taken: {sw.ElapsedMilliseconds} ms");
}
private static void Call(ITest test)
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++)
{
test.Increment();
}
sw.Stop();
Console.WriteLine($"Interface time taken: {sw.ElapsedMilliseconds} ms");
}
}
public interface ITest
{
void Increment();
}
public class Test : ITest
{
private int _count;
public void Increment()
{
_count++;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... ing-oddity