# whats the difference?!?

This topic is 4439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

look at these functions:
inline float Vec3Dot(VECTOR3 v1, VECTOR3 v2)
{
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}

and
inline float Vec3Dot(VECTOR3* v1, VECTOR3* v2)
{
return v1->x*v2->x + v1->y*v2->y + v1->z*v2->z;
}

is there any performace difference between then?!?

##### Share on other sites
The second one is faster, but I would use constant references instead of pointers:
inline float Vec3Dot(const VECTOR3& v1, const VECTOR3& v2){    return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;}

##### Share on other sites
Quote:
 Original post by xissburglook at these functions:inline float Vec3Dot(VECTOR3 v1, VECTOR3 v2){ return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;}andinline float Vec3Dot(VECTOR3* v1, VECTOR3* v2){ return v1->x*v2->x + v1->y*v2->y + v1->z*v2->z;}is there any performace difference between then?!?
The latter is probably better, but you should prefer:
inline float Vec3Dot(const VECTOR3& v1, const VECTOR3& v2){    return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;}
[Edit: hehe :) ]

##### Share on other sites
Quote:
 Original post by jykThe latter is probably better

Faster, yes, but not better. People have to use unnecessary stuff like &, which seems to make no sense. Also the function have many problems, for example the function doesn't check whether the pointers are equal to 0, the function isn't const-correct either.

Of course the reference version presented by Roboguy and jyk is the best way.

EDIT: Are you even using C++? Looking at the code it could very well be C (operator overloading not used, pointers instead of references). If you are using C then the reference version (using &) isn't legal.

##### Share on other sites
Quote:
Original post by CTar
Quote:
 Original post by jykThe latter is probably better

Faster, yes, but not better.
Sorry, you are right. What I actually meant was 'better in the context of the OP's question regarding performance', but I didn't make that clear. I should have just said 'faster'.

##### Share on other sites
its C++ and I have forgotten about using references as arguments in the functions...thx for replying man.

and, can you please explain me why?!?(if it is not too complex :)...why using 'const' makes it faster/better? and why using references also makes it faster/better?

##### Share on other sites
the first version involves the copy constructor.
the two VECTOR3 parameters are duplicated and placed on the stack before the function is called.

the second version doesn't involve the copy constructor.
only the adress of the two VECTOR3 parameters are placed on the stack before the function is called.

there's no need to make a copy of the parameters, then it's faster.

in the second version, the function works with the adress of the parameters, and then, it can modify them. after the function is executed, the parameters passed to the function can have diferent values than before the function call.
it's not the case in the first version where it works with a copy of the parameters. after the function is executed, the parameters will have the same values as before the execution.
adding const prevents the function to modify the parameters passed to it, even if they are passed by adress or by reference.

##### Share on other sites
Quote:
 Original post by xissburgits C++ and I have forgotten about using references as arguments in the functions...thx for replying man.and, can you please explain me why?!?(if it is not too complex :)...why using 'const' makes it faster/better? and why using references also makes it faster/better?

The first version passes its arguments by value, which means they are copied and the function only has access to these copies. I'm guessing VECTOR3 has three float fields x, y and z. Every float is 4 bytes long, so this would be 3*4*2=24 bytes copied in arguments.

The pointer and reference versions pass pointers; pointers being 4 bytes long, this amounts to 8 bytes. The function has access to the actual vector objects, hence any modifications made by the function can be observed after it has returned.

Using pointers is "bad" since they can be null, so in theory you're supposed to check that in the function. References are better since they work exactly the same (= just as fast), but have a nicer syntax and cannot be null.

The const does not make the code faster. It only helps you, the programmer, make less mistakes, since the "const" points out that the function will not be modifying its arguments. It's a form of documentation, it shows your intentions better.

However, the function is declared inline; although modern compilers tend to ignore that keyword AFAIK, I'm not sure what the exact effects would be if a compiler would indeed inline it.

##### Share on other sites
const = makes it safer because it ensures you don't modify the vectors
reference = makes it simpler because a reference isn't supposed to be 0 (unlike pointers)

##### Share on other sites
Quote:
 Original post by xissburgwhy using 'const' makes it faster/better?

Faster because: LIES!!! It's probably not faster, but there's a couple of optimizations your compiler can occasionally throw in when it can tell that something won't be changed (because it's const)

Better because: [18] Const correctness, C++ FAQ Lite

Quote:
 references faster/better

Faster because: Less typing, and since it's a bug to let a reference-to-null be constructed in the first place, you needn't assert() that your reference is not null (you should assert() that a pointer is not null before dereferencing it, however)

Better because: The function does not logically deal with memory - there is no new/delete/memory offset. Using pointers forces you to deal with memory, complexifying simple operations with asserts (!= null), reference (&) and dereference (*) operators, etc.

• 32
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999483
×