线程上下文切换的性能损耗测试
线程上下文切换的性能损耗到底有多少,一直没有直观的理解,今天写个程序测试一下。先看看下面的程序( 点击下载 ): ThreadTester是所有Tester的基类。所有的Tester都干的是同样一件事情,把counter增加到100000000,每次只能加1。 1: public abstract class ThreadTester 2: { 3: public const long MAX_COUNTER_NUMBER = 100000000; 4: 5: private long _counter = 0; 6: 7: //获得计数 8: public virtual long GetCounter() 9: { 10: return this._counter; 11: } 12: 13: //增加计数器 14: protected virtual void IncreaseCounter() 15: { 16: this._counter += 1; 17: } 18: 19: //启动测试 20: public abstract void Start(); 21: 22: //获得Counter从开始增加到现在的数字所耗的时间 23: public abstract long GetElapsedMillisecondsOfIncreaseCounter(); 24: