首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

.Net种创建性能测试

2012-12-21 
.Net类创建性能测试using Systemusing System.Collections.Genericusing System.Diagnosticsnamespace

.Net类创建性能测试

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();
}
}
}

热点排行