The amazing efficiency of pointer in C Programming Language
发表于2008/9/28 13:39:00 222人阅读
Today I read a book names C Programming Language and Pointer,In some section I found an amazing efficiency programme by using pointer,the example's topic is copy an array.you will see the effect after these codes compiled to assembly language.
Here is the example:
Suppose we already know to array A and B,and now we want to copy A to B,assume A and B have a same size we just define it is SIZE,the following I will show some ways of array copy.
The condition is:
#define SIZE 50
for(i = 0; i < SIZE; i++)
A[i] = B[i];
this way is very clear,it is very easy to read,but with low efficiency.
int *p1, *p2
for(int i = 0, p1 = A, p2 = B; i < SIZE; i++)
*p2++ = *p1++;
this way is clear ,too.and the efficiency will improves while comared to the preceding way.
register int *p1, *p2;
for(p1 = A, p2 = B; p1 <= &A[Size - 1];)
*p2++ = *p1++;
this is the most efficiency way for two array copy.but it is not so clear while compared to the preceding two ways.
Why C programming language is so efficiency? the most reason I think is it can manipulate the memory and harewares directly.if you wite a good C programme the excute speed is more or less the same as Assembly Language.
- The C Programming Language——Exercise solutions of the chapter one （1st）
- about the escaper character in C programming language
- C 语言高效编程的几招——A few action of efficient C language programming
- type conversion of array and pointer in C(2)
- type conversion of array and pointer in C(1)
- efficiency of optimization potential in supercritical ORC
- The answer of The C programming Language 2nd Edition
- The Answer of C Programming Language
- Learn C Programming Language in 24 Hours