How use of pointer increases Execution Speed?
It is says that use of pointers will increase the speed of
execution. Well it depends on the how and where we are using it. When we use a pointer to access a variable
then first program goes to the memory address of variable and then goes to
variable. We will understand the execution time by taking two cases in
consideration. First with passing single variable to a function and second with
passing array of data to function.
Case 1: Passing Single variable to another function
Figure below show an example of finding square of a number
by call by reference function :
int
value1=50; //value1
defination
int
*ptr1; // pointer
defination
int
value2=0; //value2 defination
ptr1=&value1; // copy value1 address to pointer
ptr1
value2=fuction1(ptr1); // call of function for square a value
.
.
/***function
for square of a number*****/
int
function1(int *pointer)
{int a=0;
a=*pointer;
return(a*a);}
value1 address will be stored in pointer ptr1. Square of a
no is found by passing the value1 to function1 using call by reference.
Function1 will calculate the square of the no and return the value to value2. I
this before calling function1 cpu calculate the address of value1 and pass it
to function. In function1 cpu first go to address, then find value at this
location and then calculate the square of number.
If we execute this function using call by value then program
will be like this:
int
value1=50; //value1
defination
int
value2=0; //value2 defination
value2=fuction1(value1); // call of function for square a value
.
.
/***function
for square of a number*****/
int
function1(int value)
{
return(value*value);}
It will transfer the value directly to function1. In
function1 value1 will be copied to value, calculate the square of no and then
return value to value2.
So call by value will take less time to execute this and
program size will be small. So for such type of operation call by value method
will be preferred.
Case 2: Passing array of data to function.
Figure below show the example of passing an array of data to
function and then return average of array data using call by value:
int
data[5]={50,40,34,23,78}; //5 byte array defination
int
avg_value=0; //
avg_value variable decleration
avg_value
= avg_fun(data[0], data[1],data[2],data[3],data[4]); //function call by value
.
.
/***function
for average of a number*****/
int
function1(int data1, int data2, int
data3,int data4, int data5)
{
Int avg=0;
avg=(data1+data2+data3+data4+data5)/5;
return(avg);
}
As per above program if we pass multiple of data from one
function to another function it will copied individually in the different location
in stack. CPU accesses the data from the stack memory and takes average of data
and return result to avg_data.
If we execute this function using pointer it will be like
this:
int
data[5]={50,40,34,23,78}; //5 byte array definition
int *ptr1;
int
avg_value=0; //
avg_value variable declaration
ptr1=&data;
avg_value
= avg_fun(ptr1); //function call by value
.
.
/***function
for average of a number*****/
int avg_fun
(int *ptr1)
{
int avg=0;
int index=0;
for(index=0;index<5;index++)
avg=avg + *ptr++;
return(avg);}
In this scenario we only pass the starting address of data
array. This data array starting address will be stored in stack memory. Cpu will
fetch the data from location using pointers, calculate the average and return the
result. So execution time will be less as compare to call by value procedure. Another advantage
of using pointer is that RAM memory used will be less.