当前位置: 首页 > 编程语言 > C#

使用属性和性能分享

时间:2023-04-10 15:38:39 C#

使用属性和性能我正在优化我的代码,我注意到使用属性(甚至是自动属性)会对执行时间产生深远的影响。请参阅以下示例:[Test]publicvoidGetterVsField(){PropertyTestpropertyTest=newPropertyTest();秒表stopwatch=newStopwatch();秒表.Start();propertyTest.LoopUsingCopy();Console.WriteLine("使用副本:"+stopwatch.ElapsedMilliseconds/1000.0);秒表.Restart();propertyTest.LoopUsingGetter();Console.WriteLine("使用getter:"+stopwatch.ElapsedMilliseconds/1000.0);秒表.Restart();propertyTest.LoopUsingField();Console.WriteLine("使用字段:"+stopwatch.ElapsedMilliseconds/1000.0);}publicclassPropertyTest{publicPropertyTest(){NumRepet=100000000;_numRepet=NumRepet;}intNumRepet{得到;放;}privateint_numRepet;publicintLoopUsingGetter(){intdummy=314;for(inti=0;i