0

I just wanted to know If using function in the programs speeds up execution time?

Say, I have simple binary search Program

#include <stdio.h>

int main()
{
   int c, first, last, middle, n, search, array[100];
   scanf("%d",&n);
   for ( c = 0 ; c < n ; c++ )
   scanf("%d",&array[c]);
   scanf("%d",&search);

   first = 0;
   last = n - 1;
   middle = (first+last)/2;

   while( first <= last )
   {
      if ( array[middle] < search )
         first = middle + 1;    
      else if ( array[middle] == search ) 
      {
         printf("%d found at location %d.\n", search, middle+1);
         break;
      }
      else
         last = middle - 1;

      middle = (first + last)/2;
   }
   if ( first > last )
      printf("Not found! %d is not present in the list.\n", search);

   return 0;   
}

And Same Program using function.

I have just added the function

int binarysearch(int *array,int m,int n)
{

    int l,u,mid;
l=0,u=n-1;
    while(l<=u)
        {
         mid=(l+u)/2;
         if(m==a[mid])
            {
             c=1;
             break;
         }
         else if(m<a[mid])
            {
             u=mid-1;
         }
         else
             l=mid+1;
    }
    return mid;
}

Code is just for understanding purpose!

Now Which will run faster? Program using Function or Iterative Programs? I'm talking in general not about any specific program.

Blackhat002
  • 598
  • 4
  • 12
  • 3
    As so often, it depends ... – πάντα ῥεῖ Nov 03 '14 at 12:07
  • 4
    Use functions when they help to make the code readable and maintainable. Do not care about their speed. Whether your algorithm is O(n), O(log n) etc etc will have 999999x more impact on your program speed. – Neil Kirk Nov 03 '14 at 12:09
  • Is this question is too broad? why close this question please specify reason? – Blackhat002 Nov 03 '14 at 12:11
  • @Blackhat002 Yes the reason was _'Too broad'_. – πάντα ῥεῖ Nov 03 '14 at 12:13
  • @πάνταῥεῖ how but I don't think, we any write Programs without using function, except recursive once. I don't think it is too board to answer! – Blackhat002 Nov 03 '14 at 12:16
  • Also don't hardcode arbitrary array sizes for user input. Pet peeve of mine. Use vector and resize it to the required size. The ironic thing is the time taken to resize the vector will be larger than the cost of calling your function and even then is negligible for this program. – Neil Kirk Nov 03 '14 at 12:18
  • 3
    @Blackhat002 Well, the only reasonable answer to your question is that sometimes functions makes things slower, and sometimes it makes things faster. That's not a very useful answer, but that's because it's not a very useful question. – nos Nov 03 '14 at 12:21
  • @nos Okay! I'm taking it down!! Just Curious mind! – Blackhat002 Nov 03 '14 at 12:23
  • 1
    @nos I disagree. I think it is a useful question. Broad, but useful. – Neil Kirk Nov 03 '14 at 12:23
  • 1
    Closed as too broad. You can write a book on this topic. That's a very good indication that the question is too broad. – MSalters Nov 03 '14 at 12:25
  • Like many people who ask about performance, you are asking the wrong question. Do not ask "Does X make programs slow?" *Do* ask "How can I find out how to make *this particular program* faster?" Here is [*my answer*](http://stackoverflow.com/a/378024/23771). – Mike Dunlavey Nov 03 '14 at 12:54
  • Otipmizing one step in software development at the expense of others is rarely effective overall. – Martin James Nov 03 '14 at 13:46

2 Answers2

9

Functions CAN make code faster by coding logic once instead of repeating several times and thus reducing code size and resulting in better CPU cache usage. Functions CAN make code slower by copying parameters and hiding info from the optimization. Certain functions can be inlined to undo these disadvantages. There are just so many variables you really cannot make a generalization.

Use functions when they help to make the code readable, maintainable and reusable.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
2

Now Which will run faster?

You can find out by measuring... But don't bother. The more relevant questions you should ask yourself are "Which is more readable?" and "Which is easier to maintain?". Consider that you might need to do a binary search in more than one place in your code.

eerorika
  • 232,697
  • 12
  • 197
  • 326