在一些需要隔时触发的场景中,如javascript中的setInterval函数,在.Net中,你用什么?
是System.Timer.Timer?
or
while(true)
{
Thread.Sleep(1000);
}
今天比较一下Timer和Sleep.
结果:Thread.sleep,问天下谁于争峰。
空间:
Type work set virtual bytes page file bytes Thread Count Handle
Timer 8.990.720 114.978.816 11.444.224 4 115
Thread.Sleep 6.590464 104.296.448 7.143.424 3 95
Thread.Sleep全胜
时间:
Type start Time Interval times finish time
Timer 11:53:37:416 10ms 100000 12:19:37:555
Thread.Sleep 11:53:37:432 10ms 100000 12:19:36:713
Timer和Thread.Sleep打个平手,但Thread.sleep还是要强那么一点
分析:
Thread.sleep直接调用内核的指令,所在线程挂起,CPU执行队列的重排序。
Timer每次Elapsed会在线程池中取新的线程来执行,存在多次访问线程池的损耗。
测试代码:
view plaincopy to clipboardprint?
class Program
{
private static System.Timers.Timer _timer;
private static volatile int _Max = 100000;
private static volatile int _ThreadMax = 100000;
static void Main(string[] args)
{
Console.WriteLine(System.DateTime.Now.ToString() + System.DateTime.Now.Millisecond.ToString());
StartTimer();
Console.WriteLine(System.DateTime.Now.ToString() + System.DateTime.Now.Millisecond.ToString());
StartThread();
Console.ReadLine();
}
static private void StartThread()
{
Thread s = new Thread(ThreadGo);
s.Start();
}
static private void ThreadGo()
{
while (true)
{
Thread.Sleep(10);
_ThreadMax--;
if (_ThreadMax < 0) break;
}
Console.WriteLine("ThreadGo" + System.DateTime.Now.ToString() + System.DateTime.Now.Millisecond.ToString());
Thread.CurrentThread.Abort();
}
static private void StartTimer()
{
_timer = new System.Timers.Timer();
_timer.AutoReset = true;
_timer.Interval = 10;
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
}
static void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
_Max--;
if(_Max<0)
{
Console.WriteLine("_timer_Elapsed" + System.DateTime.Now.ToString() + System.DateTime.Now.Millisecond.ToString());
_timer.Stop();
_timer.Dispose();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gisfarmer/archive/2009/03/13/3986277.aspx