Wednesday 15 June 2011

performance - Difference between variables inside the function and variables functioning as arguments in C++ -


My question comes from I want to compare the efficiency of the following tasks:

Function 1 < / P>

  bool my_func1 (int arg1, int and output, std :: vector & lt; int & gt; & intermediate_vec); {// something}   

When I use this function, I will call it the following way:

  int arg; Integer output; Std :: vector & lt; Integer & gt; Intermediate_vec; My_func1 (arg, output, intermediate_vec);   

Function 2

  bool my_func2 (int arg1, int and output); {Std :: vector & lt; Int & gt; Intermediate_vec my_func1 Return (arg1, Output, Intermediate Ewek); }   

When I use this function, I will call it the following way:

  int arg; Integer output; My_func2 (arg, production);   

My question is, are they two functions of the same efficiency?

This is one of those questions where it is necessary to answer the details exactly.

There should be very little difference, especially if the compiler is able to inline the func1 or func2 in order to avoid additional calling overhead (i.e., the function It is declared that they can be seen by the compiler from the calling code).

There will be only one major difference if you can avoid the creation / destruction of the intermediate_vec - just like you are using the same again in the loop (but most of the time you will see the vector " Resetting ", which is a key part of destroying vectors).

How big the difference is, as a ratio of the total time, actually depends on some in func1 is actually doing.

However, like all display queries, it is worth having a performance benchmark to compare two. Make sure you compile it from the optimization level of your actual code.

No comments:

Post a Comment