消除边界检查的 foreach 循环的特殊情况是什么? [英] What is the special case with the foreach loop that eliminates bounds checking?

查看:21
本文介绍了消除边界检查的 foreach 循环的特殊情况是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach/for 循环消除边界检查的特殊情况是什么?还有它是哪个边界检查?

What is the special case with the foreach/for loop that eliminates bounds checking? Also which bounds checking is it?

推荐答案

标准

for(int i = 0; i < array.Length; i++) {
    ...
}

循环是允许 JIT 安全删除数组边界检查的循环(索引是否在 [0..length-1] 内)

loop is the one that allows the JIT to safely remove array bounds checks (whether the index is within [0..length-1])

foreach 对数组的循环等效于标准的 for 对数组的循环.

The foreach loop over arrays is equivalent to that standard for loop over arrays.

正如罗伯特·杰普森 (Robert Jeppesen) 指出的那样:

As Robert Jeppesen points out:

如果数组是,这将被优化当地的.如果数组可以从其他位置,边界检查将仍然被执行.参考:CLR 中的数组边界检查消除

This will be optimized if the array is local. If the array is accessible from other locations, bounds checking will still be performed. Reference: Array Bounds Check Elimination in the CLR

谢谢!我自己不知道.

这篇关于消除边界检查的 foreach 循环的特殊情况是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆