cailangwei

九尺之台,起于累土。
温故而知新,可以为师矣!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

检测服务器运行时间&as和is效率之对比

Posted on 2011-12-04 17:15  cailangwei  阅读(175)  评论(0)    收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
/// <summary>
/// 检测服务器运行时间&as和is效率之对比
/// </summary>
class Class1{}

class Program
{
private Class1 c1 = new Class1();

static void Main(string[] args)
{
Program program = new Program();
Stopwatch timer = new Stopwatch();
timer.Start();
for (int i = 0; i < 10000; i++)
{
program.DoSomething1();
}
timer.Stop();
decimal micro = timer.Elapsed.Ticks/10m;
Console.WriteLine("执行DoSomeThing1()10000次的时间:{0:F1} 微秒.",micro);

timer = new Stopwatch();
timer.Start();
for (int i = 0; i < 10000; i++)
{
program.DoSomething2();
}
timer.Stop();
micro = timer.Elapsed.Ticks / 10m;
Console.WriteLine("执行DoSomeThing2()10000次的时间:{0:F1} 微秒.", micro);
}
public void DoSomething1()
{
object c2 = c1;
if (c2 is Class1)
{
Class1 c = (Class1) c2;
}
}
public void DoSomething2()
{
object c2 = c1;
Class1 c = c2 as Class1;
if (c != null)
{
//其他操作
}
}
}
}