Will using functions increase execution time ?

Will using functions increase execution time ?

I am having a doubt whether using a lot of functions actually increase your execution time. I had recently had a probelm to calculate the sum of areas of 10 circles . The problem might contain two functions one for calculating the area link float area () and another for sum like float sum () .

I have done the program without functions like :

#include

#include

#incude

void main()

{

int i;

float sum=0,a[20],r[20];

clrscr();

cout<<"Enter the radius of 10 circles \n " ;

for(i=0;i<=9;i++)

{

cin>>r[i] ;

}

for(i=0;i<=9;i++)

{

a[i]=3.14*r[i]*r[i];

sum=sum+a[i];

}

cout<<"The sum of areas of 10 circles is "<

}

I would like to get the comments on this program and the ways to improve the same .

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Best Reply

With such a small case, you won't see any difference. For larger cases, possible loss of performance with functions might be avoided by optimizations such as function in-lining and loop fusion, so you have to investigate whether those optimizations were applied effectively.

For example, you might use C++ accumulate to calculate a sum. Even though icpc in-lines the code for this, it will not fuse that loop with the one which generates the data, so you will pay the penalty of extra moves of data to and from memory.

If you are interested in performance, you should always consider whether the work to be performed will consist mainly of data copying operations, and whether you have written it in such a way as to avoid redundant copying.

Leave a Comment

Please sign in to add a comment. Not a member? Join today