You are not logged in.

Read the FAQ and Knowledge Base before posting.
We won't make a 3DS/2DS emulator.



#1 2010-06-29 17:56:34

lig
Member
Registered: 2010-04-24
Posts: 20

Pass by Reference Vs. Pass by Value

I was looking through the code for desmume and noticed that a lot of the function were pass by value. I remember learning in school years ago that if you always pass by reference instead of by value then the program will run faster. So I did an experiment today and its true that pass by reference is always faster. Heres the code I ran:

#include <iostream>
#include <time.h>
using namespace std;

void pass_by_reference(const int& a, const int& b, const int& c)
{
 
}

void pass_by_const(const int a, const int b, const int c)
{
 
}

void pass_by_value(int a, int b, int c)
{

}

int main ()
{
  time_t start,end;
  double dif;
  int a, b, c;
  //reference
  time (&start);
  for(unsigned int i = 0; i < 4294967295; i++)
    pass_by_reference(a, b, c);
  time (&end);
  dif = difftime (end,start);
  cout << "Pass by reference: " << dif << endl;
  //const
  time (&start);
  for(unsigned int i = 0; i < 4294967295; i++)
    pass_by_const(a, b, c);
  time (&end);
  dif = difftime (end,start);
  cout << "Pass by const: " << dif << endl;
  //value
  time (&start);
  for(unsigned int i = 0; i < 4294967295; i++)
    pass_by_value(a, b, c);
  time (&end);
  dif = difftime (end,start);
  cout << "Pass by value: " << dif << endl;
 
  return 0;
}

************************************************************
The output is usually 17 seconds for pass by reference, 26 for const and value, respectively.

Offline

#2 2010-06-29 19:26:11

zeromus
Radical Ninja
Registered: 2009-01-05
Posts: 5,766

Re: Pass by Reference Vs. Pass by Value

nice try, but you fail on many counts.
first of all on any respectable compiler, when doing an optimized build (you were doing an optimized build, right? or else a test like this is useless) those function calls will get optimized to nothing. literally, the program will eliminate the loops altogether.
secondly, find a way to measure with finer granularity than 1 second.
finally, your results are wrong. in this scenario pretty much any compiler should get the same results for all these cases. for one thing the examples are trivial enough that the const declaration can't help anything (it can, and does, in more complex cases); for another thing, an int& and an int are the same size. They take the same amount of time to pass to the function (and in the case of such small functions, may be getting passed in registers anyway regardless), and in fact, the int& can't be anything but slower due to the extra semantical guarantees of references vs value parameters, although once more in such a trivial case it isn't going to amount to anything.

I believe what you mean is this: rules of thumb they always teach you in school aren't right, but here's one: passing things with costly copy constructors, or things larger than the machine word as reference might be faster.

If you believe that "if you always pass by reference instead of by value then the program will run faster" then simply scour the vast majority of c++ stuff in existence and you will discover that reference parameters are relatively infrequent except when dealing with bulky objects (to avoid copying) or templates.

It costs nothing to "copy" ints.

Offline

#3 2010-06-29 21:13:32

lig
Member
Registered: 2010-04-24
Posts: 20

Re: Pass by Reference Vs. Pass by Value

Yeah it didn't make much sense that it would run faster because copying a pointer and an int should take the same amount of time because they're the same size. I have another idea, however, there are a lot of small functions in disassemble.cpp and arm_instructions.cpp would using the inline keyword in the function declarations help any?

Offline

#4 2010-06-29 21:24:11

zeromus
Radical Ninja
Registered: 2009-01-05
Posts: 5,766

Re: Pass by Reference Vs. Pass by Value

disassemble isnt normally used. arm_instructions is full of functions which are accessed via a table of function pointers. they can inline, but not as easily as just labeling them inline, and it has already been attempted anyway and got no speedup.

why dont you actually go try to make it faster and then report your results instead of guessing inappropriate things.

Offline

#5 2010-06-29 23:01:42

lig
Member
Registered: 2010-04-24
Posts: 20

Re: Pass by Reference Vs. Pass by Value

Well you know were all just hobbyists so I don't want to take anything too seriously, and I don't really know what I'm doing so all I can do is guess, but thanks for the info about disassemble not being used because I was wondering what it was for.

Offline

#6 2010-06-30 02:10:55

zeromus
Radical Ninja
Registered: 2009-01-05
Posts: 5,766

Re: Pass by Reference Vs. Pass by Value

did you try putting a breakpoint on it and seeing where it got called from?

Offline

Board footer

Powered by FluxBB