for 循环条件的最佳实践

发布于 2024-09-03 03:30:40 字数 288 浏览 4 评论 0 原文

在这种情况下什么被认为是最佳实践?

for (i=0; i<array.length(); ++i)

或者

for (i=array.length()-1; i>=0; --i)

假设我不想从某个方向迭代,而是在数组的裸长度上迭代。另外,我不打算改变循环体中数组的大小。

那么,array.length() 在编译过程中会变成常量吗?如果没有,那么第二种方法应该是首选..

what is considered best-practice in this case?

for (i=0; i<array.length(); ++i)

or

for (i=array.length()-1; i>=0; --i)

assuming i don't want to iterate from a certain direction, but rather over the bare length of the array. also, i don't plan to alter the array's size in the loop body.

so, will the array.length() become constant during compilation? if not, then the second approach should be the one to go for..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

微凉 2024-09-10 03:30:40

我会使用第一种方法,因为它更具可读性,并且我可以查看并看到您正在迭代循环。第二个花了我一秒钟:(。

只要您不修改数组,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.

你的背包 2024-09-10 03:30:40

在大多数情况下,我希望 array.length() 的实现方式为 O(1),因此它不会真正影响循环的性能。如果您有疑问,或者想确保它是一个常量,请明确地这样做:

// JavaScript
var l = a.length;

for (var i=0; i<l; i++) {
  // do something
}

我认为反转符号是属于过早优化类别的“聪明的黑客”。它更难阅读,更容易出错,并且并没有真正提供比我建议的替代方案更好的好处。

但由于编译器/解释器的实现有很大不同,并且您没有说明您指的是哪种语言,因此很难对此做出绝对的声明。我想说,除非这是代码中绝对时间关键的部分,或者以其他方式对代码运行时间做出了可衡量的贡献,并且您的基准测试表明以不同的方式进行操作可以带来真正的好处,否则我会坚持使用更容易理解和维护的代码。

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:

// JavaScript
var l = a.length;

for (var i=0; i<l; i++) {
  // do something
}

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.

灯下孤影 2024-09-10 03:30:40

版本 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.

尛丟丟 2024-09-10 03:30:40

版本 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.

贱贱哒 2024-09-10 03:30:40

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, but for (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.

梦里南柯 2024-09-10 03:30:40

版本 1 是最快的。对于(i = 0;我

Version 1 is quickest. for (i=0; i

寒尘 2024-09-10 03:30:40

对于我自己的代码中的 for 循环,虽然这可能有点过头了,但我很早就养成了编写循环的习惯,这样长度只计算一次,但循环以自然的方式进行:

int len = array.length();
for (int i=0; i<len; ++i) {
    doSomething(array[i]);
}

不过,现在我更喜欢在可用且方便的地方使用“for-each”设施;它们使循环更容易阅读并且万无一失。在 C++ 中,这将类似于:

std::for_each(array.begin(), array.end(), &doSomething);

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:

int len = array.length();
for (int i=0; i<len; ++i) {
    doSomething(array[i]);
}

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:

std::for_each(array.begin(), array.end(), &doSomething);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文