pass by value vs. pass by reference
will pass by reference be faster than a pass by value function? even if you''re not going to modify the variable inside your function? is it okay to pass everything by reference if it''s faster?
a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
A lot of how ''by reference'' compares to ''by value'' has to do with what data types you are passing. I am assuming that you aren''t changing the data, as you stated above.
So, if you''re dealing with native types like ints, et al. it probably is faster to just pass by value since the pointer you are going to pass by reference will be as big as an int and you won''t have to have the extra level of indirection.
On the other hand, if you are passing some enormous class with dozens of member variables it will most likely be MUCH faster to use pass by reference. Especially if you are only dealing with a few of that class''s members in the function
Hope that helps you out!
So, if you''re dealing with native types like ints, et al. it probably is faster to just pass by value since the pointer you are going to pass by reference will be as big as an int and you won''t have to have the extra level of indirection.
On the other hand, if you are passing some enormous class with dozens of member variables it will most likely be MUCH faster to use pass by reference. Especially if you are only dealing with a few of that class''s members in the function
Hope that helps you out!
Ok, I could be wrong about this, so someone correct me if I am... but I think that passing by value is (slightly) slower than passing by reference. As I understand it, passing by value (which prevents you from changing the value of the parameter you passed) requires -making-a-copy- of the value. Passing by reference simply uses a pointer so that whenever you refer to the variable in the function, you're actually referring to the -original- variable's memory space (the one you passed).
I'm pretty sure about all of the above... as to which is faster, I would have to say that passing by reference is -very- slightly faster. It shouldn't really make a difference speed wise I don't think unless you're doing something like repeating the function over and over in a loop.
I have a feeling that I heard someone once say that passing by reference is faster, but I'm not sure at all (and they could even have been wrong). Basically I'd only pass by reference if I purposfully intended the function to change the value of the variable(s) passed.
Anyone have anything to add/correct?
===========================================
As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.
-Albert Einstein
Edited by - Tebriel on August 1, 2000 7:22:53 PM
I'm pretty sure about all of the above... as to which is faster, I would have to say that passing by reference is -very- slightly faster. It shouldn't really make a difference speed wise I don't think unless you're doing something like repeating the function over and over in a loop.
I have a feeling that I heard someone once say that passing by reference is faster, but I'm not sure at all (and they could even have been wrong). Basically I'd only pass by reference if I purposfully intended the function to change the value of the variable(s) passed.
Anyone have anything to add/correct?
===========================================
As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.
-Albert Einstein
Edited by - Tebriel on August 1, 2000 7:22:53 PM
...but remember boys and girls, if you are not going to change an argument''s value inside the function, you can just scatter about that word of joy that we all know and love... const
I tend to agree with jaxson... for simple types, just pass them by value, but for objects/structs, pass by reference (declaring them const so the compiler kicks your arse if you try to change anything you shouldn''t)
-------------
squirrels are a remarkable source of protein...
I tend to agree with jaxson... for simple types, just pass them by value, but for objects/structs, pass by reference (declaring them const so the compiler kicks your arse if you try to change anything you shouldn''t)
-------------
squirrels are a remarkable source of protein...
allright tebriel, say you have a class that takes up 50 bytes, you pass yours by value and I will pass mine by reference (or pointer which I prefer) and we will see which one is better even if you arent going to modify the data.
And like BadMonkey said, you can put a const in front of the values you are passing by reference to make sure they dont get changed
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
And like BadMonkey said, you can put a const in front of the values you are passing by reference to make sure they dont get changed
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
No need to be rude, I stated that I wasn't 100% certain about all this.
But,
"even if you're not going to modify the variable"
he only mentioned a variable, not classes.
With this assumption, I don't think that it'll make a big difference either way speed wise.
[edit] Ok, I just tested it, and looped a function call 30000000 times. If my test was accurate, both passing by reference and by value took exactly the same amount of time, with a variable...
I'm not going to argue about the class thing...
===========================================
As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.
-Albert Einstein
Edited by - Tebriel on August 1, 2000 12:44:23 AM
But,
"even if you're not going to modify the variable"
he only mentioned a variable, not classes.
With this assumption, I don't think that it'll make a big difference either way speed wise.
[edit] Ok, I just tested it, and looped a function call 30000000 times. If my test was accurate, both passing by reference and by value took exactly the same amount of time, with a variable...
I'm not going to argue about the class thing...
===========================================
As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.
-Albert Einstein
Edited by - Tebriel on August 1, 2000 12:44:23 AM
OK here it is:
Passing by value copies all of the passed data onto the stack. Passing my reference only copies a pointer onto the stack. A pointer is 4 bytes on 32 bit systems. If your data that you are passing is larger than 4 bytes, passing by value will be ever so slightly slower than passing by reference.
The general rule is, I think, pass all simple data types (char, int, enum, float, etc) by value (unless you need to modify them inside the function and keep the modification when the function returns), and pass all complex types (classes, structs, etc..) by reference.
Passing by value copies all of the passed data onto the stack. Passing my reference only copies a pointer onto the stack. A pointer is 4 bytes on 32 bit systems. If your data that you are passing is larger than 4 bytes, passing by value will be ever so slightly slower than passing by reference.
The general rule is, I think, pass all simple data types (char, int, enum, float, etc) by value (unless you need to modify them inside the function and keep the modification when the function returns), and pass all complex types (classes, structs, etc..) by reference.
Thanks, guys.
a2k
a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
quote: Original post by IdEstVita
OK here it is:
Passing by value copies all of the passed data onto the stack. Passing my reference only copies a pointer onto the stack. A pointer is 4 bytes on 32 bit systems. If your data that you are passing is larger than 4 bytes, passing by value will be ever so slightly slower than passing by reference.
The general rule is, I think, pass all simple data types (char, int, enum, float, etc) by value (unless you need to modify them inside the function and keep the modification when the function returns), and pass all complex types (classes, structs, etc..) by reference.
That''s what I was trying to say. haha I guess I wasn''t very clear. Thanks for clearing it up, IdEstVita.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement