Function calling

I wrote a program in which I called a function which took string and int as the parameters, when I submitted the code it led to time limit exceed but when I removed the function call and inserted its code into the main code then it ran in 2 seconds. Does calling a function takes more time I even inserted the prefix inline for the function. Please explain this

Possibly if the string and the int which you were using as arguments were being passed by value, then that might explain your overhead a bit…Note that this works only if you don’t need to use the copy which is passed to the function later…

I’m not so sure about plain strings and integers, but for example doing the same with a vector can lead to some very unexpected results:

int f(vector<int> v)
  //sums elements of v

int f_modified(vector<int> & v)
   //does the same as f, but the speed increase is dramatic if v is a large vector

You can try this for yourself with a simple program… I’m sure you will be surprised :stuck_out_tongue:

Also, if the size of your string is fixed and const, use a variable to store it, instead of doing:

for(int i = 0; i < string.size(); i++)

this also adds a DRAMATIC overhead in time…



I don’t guess calling a function should take more time. It would be easier for me or anyone else on forum to check the problem if you share the code… :slight_smile: