for 循环条件的最佳实践
在这种情况下什么被认为是最佳实践?
for (i=0; i<array.length(); ++i)
或者
for (i=array.length()-1; i>=0; --i)
假设我不想从某个方向迭代,而是在数组的裸长度上迭代。另外,我不打算改变循环体中数组的大小。
那么,array.length()
在编译过程中会变成常量吗?如果没有,那么第二种方法应该是首选..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会使用第一种方法,因为它更具可读性,并且我可以查看并看到您正在迭代循环。第二个花了我一秒钟:(。
只要您不修改数组,
array.length
将保持不变。I would do the first method, as that is much more readable, and I can look and see you are iterating over the loop. The second one took me a second :(.
array.length
will remain constant so long as you arent modifying the array.在大多数情况下,我希望 array.length() 的实现方式为 O(1),因此它不会真正影响循环的性能。如果您有疑问,或者想确保它是一个常量,请明确地这样做:
我认为反转符号是属于过早优化类别的“聪明的黑客”。它更难阅读,更容易出错,并且并没有真正提供比我建议的替代方案更好的好处。
但由于编译器/解释器的实现有很大不同,并且您没有说明您指的是哪种语言,因此很难对此做出绝对的声明。我想说,除非这是代码中绝对时间关键的部分,或者以其他方式对代码运行时间做出了可衡量的贡献,并且您的基准测试表明以不同的方式进行操作可以带来真正的好处,否则我会坚持使用更容易理解和维护的代码。
In most cases I would expect
array.length()
to be implemented in such a way that it is O(1), so it would not really impact on the loop's performance. If you are in doubt, or want to make sure it is a constant, just do so explicitly:I consider the reversed notation a "clever hack" that falls into the premature optimization category. It's harder to read, more error-prone and does not really provide a benefit over the alternative I suggest.
But since implementations of compilers/interpreters are vastly different and you do not say what language you refer to, it is hard to make an absolute statement about this. I would say unless this is in an absolutely time-critical section of code or otherwise measurably contributing to code running time, and your benchmark tests show that doing it differently provides a real benefit, I would stick to the code that's easier to understand and maintain.
版本 2 已损坏,将从数组的过去一端迭代到 1。(现已更正)
坚持使用版本 1。它已被广泛认可,不会让读者再三考虑。
Version 2 is broken and would iterate from one past end of array to 1. (Now corrected)
Stick with version 1. It's well recognised and doesn't leave the reader doing a double-take.
版本 1 使用更广泛,也更容易理解。如果编译器没有将 array.length() 优化为常量,版本 2 有时可能会稍快一些,但是...
在此处插入您自己的过早优化注释
编辑:至于 array.length 是否() 将被优化掉,这取决于语言。如果语言使用数组作为“普通”对象或数组可以动态调整大小,则它只是一个方法调用,编译器不能假设将返回一致的返回值。但对于数组是特殊情况或对象的语言(或者编译器真的很聪明......),速度差异可能会被消除。
Version 1 is far more widely used and simpler to understand. Version 2 may occasionally be very slightly faster if the compiler doesn't optimize array.length() into a constant, but...
insert your own premature optimization comment here
EDIT: as to whether array.length() will be optimized out, it will depend on the language. If the language uses arrays as "normal" objects or arrays can be dynamically sized, it will be just a method call and the compiler can't assume will return a consistent return value. But for languages in which arrays are a special case or object (or the compiler's just really smart...) the speed difference will probably be eliminated.
for (i=0; i 对我来说更好,但是
for (i=array.length()-1; i>=0 ; --i)
是最快的,因为通用处理器最快地检查与 0 比较的条件和与两个变量比较的条件。如果您在迭代过程中不添加/删除此数组中的元素,则
array.lenght()
是常量。for (i=0; i<array.length(); ++i)
is better for me, butfor (i=array.length()-1; i>=0; --i)
is fastest, because common processors fastest checking condition comparing to 0 to condition comparing to two varbiales.array.lenght()
is constant if you dont adding/erasing elements from this array under iterations.版本 1 是最快的。对于(i = 0;我
Version 1 is quickest. for (i=0; i
对于我自己的代码中的 for 循环,虽然这可能有点过头了,但我很早就养成了编写循环的习惯,这样长度只计算一次,但循环以自然的方式进行:
不过,现在我更喜欢在可用且方便的地方使用“for-each”设施;它们使循环更容易阅读并且万无一失。在 C++ 中,这将类似于:
For a for loop in my own code, though it's probably overkill, I got into the habit early on of writing my loops such that the length gets calculated exactly once, but the loop proceeds in the natural way:
These days, though, I prefer using "for-each" facilities where they're available and convenient; they make loops easier to read and foolproof. In C++ that would be something like: