using
System;
using System.Collections.Generic;
using System.Diagnostics;
namespace ConsoleApplication5_Inject
{
class Entity { }
class Program
{
static void Main( string [] args)
{
#region Run1
Stopwatch sw = new Stopwatch();
sw.Start();
for ( int i = 0 ; i < 100000 ; i ++ )
{
Run1();
}
sw.Stop();
Console.WriteLine(sw.Elapsed.Milliseconds);
#endregion
#region Run2
sw = new Stopwatch();
sw.Start();
for ( int i = 0 ; i < 100000 ; i ++ )
{
Run2();
}
sw.Stop();
Console.WriteLine(sw.Elapsed.Milliseconds);
#endregion
#region Run3
sw = new Stopwatch();
sw.Start();
for ( int i = 0 ; i < 100000 ; i ++ )
{
Run3();
}
sw.Stop();
Console.WriteLine(sw.Elapsed.Milliseconds);
#endregion
#region Run4
sw = new Stopwatch();
sw.Start();
for ( int i = 0 ; i < 100000 ; i ++ )
{
Run4 < Entity > ();
}
sw.Stop();
Console.WriteLine(sw.Elapsed.Milliseconds);
#endregion
Console.ReadKey();
}
static void Run1()
{
new Entity();
}
static void Run2()
{
Activator.CreateInstance( typeof (Entity));
}
static void Run3()
{
Activator.CreateInstance < Entity > ();
}
static void Run4 < T > () where T : new ()
{
new T();
}
}
}
using System.Collections.Generic;
using System.Diagnostics;
namespace ConsoleApplication5_Inject
{
class Entity { }
class Program
{
static void Main( string [] args)
{
#region Run1
Stopwatch sw = new Stopwatch();
sw.Start();
for ( int i = 0 ; i < 100000 ; i ++ )
{
Run1();
}
sw.Stop();
Console.WriteLine(sw.Elapsed.Milliseconds);
#endregion
#region Run2
sw = new Stopwatch();
sw.Start();
for ( int i = 0 ; i < 100000 ; i ++ )
{
Run2();
}
sw.Stop();
Console.WriteLine(sw.Elapsed.Milliseconds);
#endregion
#region Run3
sw = new Stopwatch();
sw.Start();
for ( int i = 0 ; i < 100000 ; i ++ )
{
Run3();
}
sw.Stop();
Console.WriteLine(sw.Elapsed.Milliseconds);
#endregion
#region Run4
sw = new Stopwatch();
sw.Start();
for ( int i = 0 ; i < 100000 ; i ++ )
{
Run4 < Entity > ();
}
sw.Stop();
Console.WriteLine(sw.Elapsed.Milliseconds);
#endregion
Console.ReadKey();
}
static void Run1()
{
new Entity();
}
static void Run2()
{
Activator.CreateInstance( typeof (Entity));
}
static void Run3()
{
Activator.CreateInstance < Entity > ();
}
static void Run4 < T > () where T : new ()
{
new T();
}
}
}
跑10万次 结果:
1. 3ms
2. 220ms
3. 117ms
4. 121ms