, , . . , (, ), IO, . , ( ). , , , ..
:
static: 154ms
instance: 156ms
, 2 50 ! ...
:
class Program
{
static void Main()
{
StaticMethod();
Program p = new Program();
p.InstanceMethod();
const int LOOP = 50000000;
Stopwatch watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++) StaticMethod();
watch.Stop();
Console.WriteLine("static: " + watch.ElapsedMilliseconds + "ms");
watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++) p.InstanceMethod();
watch.Stop();
Console.WriteLine("instance: " + watch.ElapsedMilliseconds + "ms");
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
void InstanceMethod() { }
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
static void StaticMethod() { }
}
:
(), 20 (if (i % 20 == 0) p = new Program();), :
static: 174ms
instance: 873ms
- , , 50 , !