The amazing efficiency of pointer in C Programming Language

      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
      int A[SIZE]
      int B[SIZE]
NO 1
void copy1()
{
     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.

NO 2
void copy2()
{
       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.

NO 3|
void copy3()
{
       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.

發佈了30 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章