Class 创建性能测试

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

跑10万次 结果:

1. 3ms

2. 220ms

3. 117ms

4. 121ms

转载于:https://www.cnblogs.com/LinFx/archive/2011/05/24/2123676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值