Pointers on C——8 Arrays.4

来源:互联网 发布:指环王和哈利波特 知乎 编辑:程序博客网 时间:2024/06/04 09:34

8.1.3 Pointers versus Subscripts

If you can use indirection expressions and subscripts interchangeably, which should you use? As usual, there is no simple answer. Subscripts are easier for ʺmost people to understand, especially with multidimensional arrays, so there is some benefit gained in readability by using them. On the other hand, this choice may affect runtime efficiency.

如果你可以互换地使用指针表达式和下标表达式,那么你应该使用哪一个呢?和往常一样,这里并没有一个简明答案。对于绝大多数人而言,下标更容易理解,尤其是在多维数组中。所以,在可读性方面,下标有一定的优势。但在另一方面,这个选择可能会影响运行时效率。


Assuming that they are both used correctly, subscripts are never more efficient than pointers, but pointers are  sometimes more efficient than subscripts.

假定这两种方法都是正确的,下标绝不会比指针更有效率,但指针有时会比下标更有效率。


To understand this efficiency issue letʹs examine two loops that perform the same work. First, we clear the elements of an array using subscripts.

为了理解这个效率问题,让我们来研究两个循环,它们用于执行相同的任务。首先,我们使用下标方案将数组中的所有元素都设置为0 。


int array[10], a;

for( a = 0; a < 10; a += 1 )

array[a] = 0;


To evaluate this subscript, the compiler inserts instructions in the program to take the value of a and multiply it by the size of an integer (say, four). This multiplication takes both space and time.

为了对下标表达式求值,编译器在程序中插入指令,取得a 的值,并把它与整型的长度(也就是4) 相乘。这个乘法需要花费一定的时间和空间。


Now consider this loop, which does exactly the same work.

现在让我们再来看看下面这个循环,它所执行的任务和前面的循环完全一样。


int array[10], *ap;

for( ap = array; ap < array + 10; ap++ )

*ap = 0;


The multiplication is still in here somewhere, even though there is no longer any subscript. Look closely now and see if you can find it.

尽管这里并不存在下标, {且还是存在乘法运算。请仔细观察一下,看看你能不能找到它。


The multiplication is now performed in the adjustment step of the for statement; the value one must be scaled to the size of an integer before it is added to the pointer. But there is a big difference here: the same two values (1 × 4) are multiplied each time through the loop. As a result, this multiplication is performed once at compile time—the program now contains an instruction to add four to the pointer. No multiplications are performed at run time.

现在,这个乘法运算出现在for 语旬的调整部分。1 这个值必须与整型的长度相乘,然后再与指针相加。但这里存在一个重大区别:循环每次执行时,执行乘法运算的都是两个相同的数(1和4)。结果,这个乘法只在编译时执行一次——程序现在包含了一条指令,把4 与指针相加。程序在运行时并不执行乘法运算。


This example illustrates the type of situation in which pointers are more

efficient than subscripts – when moving through an array by one (or any other fixed amount). The multiplication is performed on the fixed amount at compile time, so there are fewer instructions to execute at run time. On most machines the program is smaller and faster.

这个例子说明了指针比下标更有效率的场合——当你在数组中1 次l 步(或某个固定的数字)地移动时,与固定数字相乘的运算在编译时完成,所以在运行时所需的指令就少一些。在绝大多数机器上,程序将会更小一些、更快一些。


Now consider these two fragments of code:

现在考虑下面两个代码段:


There is no difference in the code generated for these statements. a could be any value, so the multiplication instruction is needed in both places to scale it. This example illustrates the type of situation in which pointers and subscripts are equally efficient.

两边的语句所产生的代码并无区别。a 可能是任何值,在运行时方知。所以两种方案都需要乘法指令,用于对a 的值进行调整。这个例子说明了指针和下标的效率完全相同的场合。


上一章 Pointers on C——8 Arrays.3