文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Class 创建性能测试

Class 创建性能测试

时间:2011-05-24  来源:林松斌

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

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载