Is it costly to do array.length or list.count in a loop

前端 未结 6 653
梦谈多话
梦谈多话 2020-12-10 01:18

I know that in JavaScript, creating a for loop like this: for(int i = 0; i < arr.length; i++) is costly as it computes the array length each time. Is this be

6条回答
  •  粉色の甜心
    2020-12-10 02:10

    1. All .Net arrays have a field containing the length of the array, so the length is not computed at usage but at creation time.

    2. The .Net virtual machine is very good at eliminating bounds checks whenever possible, this is one of those cases, where the bounds check is moved outside the loop (in most situations, and if not it's just 2 instructions overhead).

    Edit:

    Array Bounds Check Elimination

提交回复
热议问题