Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

Will using functions increase execution time ?

Cijo_Abraham_Mani
315 Views

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 ;

}

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

{

a=3.14*r*r;

sum=sum+a;

}

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 .

0 Kudos
1 Solution
TimP
Honored Contributor III
315 Views

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.

View solution in original post

0 Kudos
1 Reply
TimP
Honored Contributor III
316 Views

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.

0 Kudos
Reply