本文介绍了C#执行速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在现有旧项目中进行以下优化以提高执行速度 我需要将计数精确地分成9个分区,如果包含任何余数应该追加到最后一个分区。我已经创建了两个函数,理想情况下都是相同的。建议哪个是最好的实现方式和原因。 功能1 public static int trustcheck( int totalRows, int rowCount) { if (rowCount!= 0 &&(rowCount%(totalRows / 9 ))。等于( 0 )) { if (((totalRows%(totalRows / 9 ))+ rowCount).Equals(totalRows)) { return 0 ; } 其他 { 返回 1 ; } } else { if (rowCount.Equals(totalRows)) return 1 ; else return 0 ; } } 功能2 public static int trustcheck2( int totalRows, int rowCount) { return (rowCount! = 0 &&(rowCount%(totalRows / 9 ))。等于( 0 ))? (((totalRows%(totalRows / 9 ))+ rowCount).Equals(totalRows)? 0 : 1 ):((rowCount.Equals(totalRows))? 1 : 0 ); } 主要方法 static void Main( string [] args) { trustcheck( 105 , 105 ); ArrayList lst = new ArrayList(); for ( int k = 0 ; k < = 105 ; k ++) { if (trustcheck2( 105 ,k)> 0 ) { lst.Add(k); } } } 解决方案 您没有完全描述您想要实现的目标。代码示例不是定义。至于执行时间,请自行计算算法时间。对于计时,请使用能够提供最佳准确度的类, System.Diagnostics.Stopwatch : http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx [ ^ ]。 要了解准确性(非常非常好),请参阅: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution.aspx [ ^ ], http://msdn.microsoft.com/en -us /库/系统.diagnostics.stopwatch.frequency.aspx [ ^ ]。 您必须避免一个大陷阱:JIT编译。如果你只是立即测量你的时间,那么很大一部分将是JIT编译的时间。它通常按照每个方法进行,在每个方法的第一次调用时,因此,在计时之前,只调用一次有问题的方法,然后将它计入一系列后续调用。 另请参阅: http://en.wikipedia.org/wiki/Just-in-time_compilation [ ^ ]。 祝你好运, -SA I am doing the below optimization in existing old project to increase speed of executionI need to split the count exactly into 9 partitions if contains any remainder it should be appended to last partition. I have created two functions ideally both are same.Kindly suggest which will be the best way to implement and why.Function 1public static int trustcheck(int totalRows, int rowCount) { if (rowCount != 0 && (rowCount % (totalRows / 9)).Equals(0)) { if (((totalRows % (totalRows / 9)) + rowCount).Equals(totalRows)) { return 0; } else { return 1; } } else { if (rowCount.Equals(totalRows)) return 1; else return 0; } }Function2public static int trustcheck2(int totalRows, int rowCount){ return (rowCount != 0 && (rowCount % (totalRows / 9)).Equals(0)) ? (((totalRows % (totalRows / 9)) + rowCount).Equals(totalRows) ? 0 : 1) : ((rowCount.Equals(totalRows)) ? 1 : 0);}Main Methodstatic void Main(string[] args) { trustcheck(105, 105); ArrayList lst = new ArrayList(); for (int k = 0; k <= 105; k++) { if (trustcheck2(105, k) > 0) { lst.Add(k); } }} 解决方案 You did not completely describe what you want to achieve. The code sample is not the definition. As to the time of execution, please time your algorithms yourself. For timing, use the class which gives you the best possible accuracy, System.Diagnostics.Stopwatch:http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].To learn the accuracy (which is very, very good), please see:http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution.aspx[^],http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency.aspx[^].There is one big trap you will have to avoid: JIT compilation. If you just measure your timing immediately, big part of it will be the time for JIT compilation. It usually happens per method, at the moment of the first call of each, so, before timing, call a method in question just once, and them time it on a number of subsequent calls.See also: http://en.wikipedia.org/wiki/Just-in-time_compilation[^].Good luck,—SA 这篇关于C#执行速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 09:44