testWithAccessUsingProperties

testWithAccessUsingProperties

[直接访问]总和为:8000000000 in:00:00:10.7031250 sec。 [使用属性访问]总和为:8000000000 in:00:00:10.5937500 sec。 [使用getter访问]总和是:8000000000 in:00:00:09.6875000 sec。 [使用getters中的缓存值访问]总和为:8000000000 in: 00:00:10.7031250秒。 哇 - 它们几乎等同,最糟糕的是调试。表演者比其他方法略微优于。明白了吗。在判断代码性能时,第一印象可能是欺骗性的。对于 生产代码,基本上没有性能权衡选择 一种访问另一种风格的情况类似于示例代码我 跑了。不要相信任何告诉你性能影响的人。 选择最适合你和你的团队的编码风格,然后按照它进行操作。 一旦你有一个应用程序一直 遵循你已经测量并确定了真正的b 性能瓶颈的风格,就会担心性能。 兰迪 示例代码如下: ===== ============================================= ==== 使用系统; 名称空间CachedVariableProfile { 公共类计划 { static void Main(string [] args) { testWithCachedGetterValues(); testWithAccessUsingGetters (); testWithAccessUsingProperties(); testWithDirectAccess(); testWithDirectAccess(); testWithAccessUsingProperties(); testWithAccessUsingGett ers(); testWithCachedGetterValues(); } public static void testWithAccessUsingProperties() { Container dataSupplier = new Container(3,5); long sum = 0; DateTime endTime; DateTime startTime = DateTime.Now; for(int i = 0;我< 10亿; i ++) { sum + = dataSupplier.MyValue.X; sum + = dataSupplier.MyValue.Y; } endTime = DateTime.Now; System.Console.Out.WriteLine(" [使用属性访问]总和 是:" + sum.ToString()+" in:" +(endTime - startTime)+" sec。"); } public static void testWithAccessUsingGetters() { Container dataSupplier = new Container(3,5); long sum = 0; DateTime endTime; DateTime startTime = DateTime.Now; for(int i = 0; i< 1000000000; i ++) { sum + = dataSupplier.getX(); sum + = dataSupplier.getY(); } endTime = DateTime.Now; System.Console .Out.WriteLine([使用getters访问]总和是: " + sum.ToString()+" in:" +(endTime - startTime)+sec。 "); } public static void testWithCachedGetterValues() { Container dataSupplier = new Container(3 ,5); long sum = 0; DateTime endTime; DateTime startTime = DateTime.Now; int x = dataSupplier.getX(); int y = dataSupplier.getY(); for(int i = 0;我< 10亿; i ++) { sum + = x; sum + = y; } endTime = DateTime.Now; System.Console.Out.WriteLine(" [使用来自 的缓存值访问getters]总和是:" + sum.ToString()+" in:" +(endTime - startTime)+ " sec。"); } public static void testWithDirectAccess() { Container dataSupplier = new Container(3,5); long sum = 0; DateTime endTime; DateTime startTime = DateTime.Now; for(int i = 0; i< 1000000000; i ++) { sum + = dataSupplier.myValue.x; sum + = dataSupplier.myValue.y; } endTime = DateTime.Now; System.Console.Out.WriteLine(" [Direct Access]总和是:" + sum.ToString()+" in:" +(endTime - startTime)+"秒。;; } } 公共舱容器 { 公共Containee myValue; public Container(int x,int y) { myValue = new Containee(x,y); } 公共Containee MyValue { get { 返回myValue; } } public int X { get { 返回myValue.X; } 设置 { myValue.X = value; } } public int Y { get { 返回myValue.Y; } set { myValue.Y = value; } } 公共Containee getMyValue() { 返回myValue; } public int getX() { 返回getMyValue()。getX(); } public int getY() { 返回getMyValue()。getY(); } } 公共类Containee { public int x; public int y; public Containee(int x,int y ) { X = x; this.y = y; } public int X { get { return x; } 套装 { x =价值; } } public int Y { get { 返回y; } 设定 { y = value; } } public int getX() { 返回x; } public int getY() { 返回y; } } }I have a story to tell -- with data. I hope that doesn''t scare anyone off.The question posed by matko has at least two aspects; 1) style and 2)performance. Style is the first most important aspect when writingproduction code. You (and your team) should pick a style and then make surethat all code follows that style. Having all code written in the same stylewill be of unmeasurable value for as long as the code lives.At some point, performance may become an issue for some parts of the code.But first impressions are deceiving; especially in this case.To illustrate this point, I put together a project in MS VisualStudio 2005(Beta 2 release) that shows the performance difference between 1) directaccess, 2) access through properties, 3) access through getters and 4)cached values retrieved from getters. I will include the code at the end ofthis post. Note that I am running the code on a 2.8GHz HP Pentium 4 Laptop.The code adds 3 and 5 in a loop that cycles 1000000000 times. The resultingoutput when running the code in the IDE using the Debug build is:[Access using cached values from getters] The sum is: 8000000000 in :00:00:12.6093750 sec.[Access using getters] The sum is: 8000000000 in : 00:00:53.2031250 sec.[Access using properties] The sum is: 8000000000 in : 00:00:38.1093750 sec.[Direct Access] The sum is: 8000000000 in : 00:00:07.3750000 sec.[Direct Access] The sum is: 8000000000 in : 00:00:07.3281250 sec.[Access using properties] The sum is: 8000000000 in : 00:00:37.8437500 sec.[Access using getters] The sum is: 8000000000 in : 00:00:53.1562500 sec.[Access using cached values from getters] The sum is: 8000000000 in :00:00:12.5625000 sec.Note that I ran the code twice, once in the given order and then anothertime in the reverse order as a sanity check. This output shows that directaccess is almost twice as fast as the best of the other approaches. Howmuch impact does running the debug code in the IDE have? The resultingoutput was obtained by running the Debug build from a command prompt:[Access using cached values from getters] The sum is: 8000000000 in :00:00:12.5937500 sec.[Access using getters] The sum is: 8000000000 in : 00:00:45.5937500 sec.[Access using properties] The sum is: 8000000000 in : 00:00:36.9843750 sec.[Direct Access] The sum is: 8000000000 in : 00:00:07.2968750 sec.[Direct Access] The sum is: 8000000000 in : 00:00:07.2968750 sec.[Access using properties] The sum is: 8000000000 in : 00:00:37.1093750 sec.[Access using getters] The sum is: 8000000000 in : 00:00:46.1406250 sec.[Access using cached values from getters] The sum is: 8000000000 in :00:00:12.5781250 sec.We might expect that the results should get a little better because there isno IDE overhead. It is interesting that the worst performer improved themost. This output shows that direct access is still the clear performancewinner, however. Now examine the output when running the Release build froma command prompt:[Access using cached values from getters] The sum is: 8000000000 in :00:00:10.7031250 sec.[Access using getters] The sum is: 8000000000 in : 00:00:09.7031250 sec.[Access using properties] The sum is: 8000000000 in : 00:00:10.6406250 sec.[Direct Access] The sum is: 8000000000 in : 00:00:10.7031250 sec.[Direct Access] The sum is: 8000000000 in : 00:00:10.7031250 sec.[Access using properties] The sum is: 8000000000 in : 00:00:10.5937500 sec.[Access using getters] The sum is: 8000000000 in : 00:00:09.6875000 sec.[Access using cached values from getters] The sum is: 8000000000 in :00:00:10.7031250 sec.Wow -- they are all nearly equivalent and the worst "debug" performer has aslight edge over the other approaches. See what I mean. When it comes tojudging performance of code, first impressions can be deceiving. Forproduction code, there is essentially no performance tradeoff for choosingone style of accessing over another for case analogous to the example code Iran. Don''t believe anyone who tells you that there is a performance impact.Pick the coding style that is best for you and your team and then follow it.And worry about performance once you have an application that consistentlyfollows that style where you have measured and identified the realperformance bottlenecks.Regards,RandyExample code follows:================================================== ====using System;namespace CachedVariableProfile{public class Program{static void Main(string[] args){testWithCachedGetterValues();testWithAccessUsingGetters();testWithAccessUsingProperties();testWithDirectAccess();testWithDirectAccess();testWithAccessUsingProperties();testWithAccessUsingGetters();testWithCachedGetterValues();}public static void testWithAccessUsingProperties(){Container dataSupplier = new Container(3, 5);long sum = 0;DateTime endTime;DateTime startTime = DateTime.Now;for (int i = 0; i < 1000000000; i++){sum += dataSupplier.MyValue.X;sum += dataSupplier.MyValue.Y;}endTime = DateTime.Now;System.Console.Out.WriteLine("[Access using properties] The sumis: " + sum.ToString() + " in : " + (endTime - startTime) + " sec.");}public static void testWithAccessUsingGetters(){Container dataSupplier = new Container(3, 5);long sum = 0;DateTime endTime;DateTime startTime = DateTime.Now;for (int i = 0; i < 1000000000; i++){sum += dataSupplier.getX();sum += dataSupplier.getY();}endTime = DateTime.Now;System.Console.Out.WriteLine("[Access using getters] The sum is:" + sum.ToString() + " in : " + (endTime - startTime) + " sec.");}public static void testWithCachedGetterValues(){Container dataSupplier = new Container(3, 5);long sum = 0;DateTime endTime;DateTime startTime = DateTime.Now;int x = dataSupplier.getX();int y = dataSupplier.getY();for (int i = 0; i < 1000000000; i++){sum += x;sum += y;}endTime = DateTime.Now;System.Console.Out.WriteLine("[Access using cached values fromgetters] The sum is: " + sum.ToString() + " in : " + (endTime - startTime) +" sec.");}public static void testWithDirectAccess(){Container dataSupplier = new Container(3, 5);long sum = 0;DateTime endTime;DateTime startTime = DateTime.Now;for (int i = 0; i < 1000000000; i++){sum += dataSupplier.myValue.x;sum += dataSupplier.myValue.y;}endTime = DateTime.Now;System.Console.Out.WriteLine("[Direct Access] The sum is: " +sum.ToString() + " in : " + (endTime - startTime) + " sec.");}}public class Container{public Containee myValue;public Container(int x, int y){myValue = new Containee(x, y);}public Containee MyValue{get{return myValue;}}public int X{get{return myValue.X;}set{myValue.X = value;}}public int Y{get{return myValue.Y;}set{myValue.Y = value;}}public Containee getMyValue(){return myValue;}public int getX(){return getMyValue().getX();}public int getY(){return getMyValue().getY();}}public class Containee{public int x;public int y;public Containee(int x, int y){X = x;this.y = y;}public int X{get{return x;}set{x = value;}}public int Y{get{return y;}set{y = value;}}public int getX(){return x;}public int getY(){return y;}}} 这篇关于为什么要创建对象的本地版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 08:32