1

Edited: Please note, when I mention performance comparison, it is not performance comparison of C++ pass-by-reference vs. pass-by-value. It is more to test whether the underlying algorithm of CPLEX (to solve linear programs) runs better when CPLEX objects are passed by value or passed by reference.

Hello All:

I need to test the performance of a program when a function's parameters are passed by value (PBV) and when the same function's parameters are passed by reference (PBR). There is a library I am using (IBM ILOG CPLEX, used for Operations Research/Linear Programming related work) where objects of classes in that library can be PBV and PBR as per the library's manual which unfortunately is silent about the efficacy of one over the other. I just want to test whether the choice of one over the other may lead to performance issues.

A brute-force way to test this might be to have two source code files, source_pbv.cpp and source_pbr.cpp, where the former has all functions PBV and the latter has all functions PBR. This is pretty difficult to maintain. I was wondering how, if at all, I can go about testing the performance using a single .cpp file.

For instance, in the header file, could I define something like

#define PBV 0 //0 value here indicates I want to PBR, 1 value here indicates I want to PBV

Once this is defined, is something like the following valid in the function declaration section?

#if PBV == 0 // I want to PBR
     function_to_call(Class1& class1object);//PBR function declaration
#elif PBV == 1 // I want to PBV
     function_to_call(Class1 class1object);//PBV function declaration
#endif

Then, in the source code, I can simply have

function_to_call(class1object);

and whether this call translates into a PBV or PBR would depend on #define PBV I have in the header section.

If this is not the right way to go, any pointers to the right direction would be highly appreciated.

Also, if this way is decent, wouldn't I have to specify in the body of my code the following:

function_to_call(Class1& class1object){//Actual function details for PBR
    //Stuff
}

and

function_to_call(Class1 class1object){//Actual function details for PBV
    //Stuff
}

with //Stuff being repeated twice? Is there a way this can be avoided somehow?

Thanks.

Tryer
  • 3,580
  • 1
  • 26
  • 49

3 Answers3

4

Your solution appears ok, however it won't allow you to test PBV vs PBR in a single run (as switching from one 'mode' to the other will require a recompilation). Another solution would be to make the parameter type of function_to_call a template argument. The result will essentially be the same, but you will be able to call function_to_call<Class1> (PBV) and function_to_call<Class1 &> (PBR) from your code and let the compiler instantiate both versions.

icecrime
  • 74,451
  • 13
  • 99
  • 111
  • Hmm...If I go down the way I mentioned in the original post, I realize I really cannot get around defining fully function_to_call(Class1& class1object){//Stuff//} and repeating the same //Stuff// within function_to_call(Class1 class1object){//Stuff//}. This (unnecessarily repeating //Stuff//) is precisely what I did not want to do in the first place. Maybe what you mention, template argument, is the way to go to prevent repetition of //Stuff// within two functions that differ in their argument list only. I will look into that. Thanks. – Tryer Nov 08 '10 at 16:10
1

Another possibility (that requires re-compilation) is to use typedefs

Luca Martini
  • 1,434
  • 1
  • 15
  • 35
0

Here is what worked for me...

#define PBV 0  //0 means PBR, 1 means PBV
#if PBV == 0
    #define PCLASS1 Class1&
#elif PBV == 1
    #define PCLASS1 Class1
#endif

void function_to_call(PCLASS1 class1object){
//depending on PBV, PCLASS1 above will be either Class1& or Class1
    //Stuff
}

This way, I did not have to have //Stuff in two functions. I just need to change my code in one location to switch between PBR and PBV.

Thanks for your help.

Tryer
  • 3,580
  • 1
  • 26
  • 49