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)
{
//其他操作
}
}
}
}