Jump to content
  • Advertisement
Sign in to follow this  
Mutilate

Result as parameter (C++)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering if this actually improve performance by making less local copies on the stack of big data class Matrix4f { float _[4][4]; ... void Inverse(Matrix4 &result) const { result._[0][0] = ... } ... }; instead of: class Matrix4f { float _[4][4]; ... Matrix4f Inverse() const { Matrix4f result; result._[0][0] = ... ... return result; } ... }; one called as: Matrix4f matrix0, matrix1; matrix0 = <something>; matrix0.Inverse(matrix1); and the other: Matrix4f matrix0, matrix1; matrix0 = <something>; matrix1 = matrix0.Inverse(); And what about if the methods are inline?

Share this post


Link to post
Share on other sites
Advertisement
I wouldn't do either. Invert 'this' (the current object) instead of the object passed into the function.
class Matrix4f
{
float _[4][4];
...
void Invert()
{
_[0][0] = ...
...
}
...
};



Use:
Matrix4f mat;
...
mat.Invert();

Share this post


Link to post
Share on other sites
Yes, in other situations this would save on the time needed for stack-based creation.

In this situation though, scottdewald makes a good point.

Share this post


Link to post
Share on other sites
Yes, if you want to reduce the number of coppies made though you should look into expression templates, they are slightly harder to implement but in general result in code that allows for a) better register allocation and b) produces the smallest number of tempoaries possible.

Share this post


Link to post
Share on other sites
Quote:
Original post by scottdewald
I wouldn't do either. Invert 'this' (the current object) instead of the object passed into the function.
That's indeed a better solution. I implemented the method .Invert() as well but I also need to have the inverse and preserve the original matrix so with only the method .Invert() I'll end up with
Matrix4f matrix0, matrix1;
matrix0 = <something>;
matrix1 = matrix0;
matrix1.Invert();
which ends up with a whole copy added to the temps I need to compute the inverse.

Quote:
Original post by taby
Yes, in other situations this would save on the time needed for stack-based creation.
Excellent :)

Quote:
Original post by Julian90
Yes, if you want to reduce the number of coppies made though you should look into expression templates, they are slightly harder to implement but in general result in code that allows for a) better register allocation and b) produces the smallest number of tempoaries possible.
Very interesting reading. Thanks I'll try it out.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mutilate
one called as:
Matrix4f matrix0, matrix1;
matrix0 = <something>;
matrix0.Inverse(matrix1);

and the other:
Matrix4f matrix0, matrix1;
matrix0 = <something>;
matrix1 = matrix0.Inverse();



Just to add in another possible solution besides the matrix inverting itself. Some libraries have certain member functions that are declared as static. So imagine your matrix class:


class Matrix4f
{
float _[4][4];

static void Inverse(Matrix4f& mat);
};




To call it:


Matrix4f matrix0, matrix1;
...
matrix1 = matrix0;
Matrix4f.Inverse(matrix1);



Share this post


Link to post
Share on other sites
Quote:
Original post by GaryNas
Just to add in another possible solution besides the matrix inverting itself. Some libraries have certain member functions that are declared as static.
Yep that's another possibility. I use static functions for instance to create givens rotations matrices out of 3 angles, something like:
class Matrix3f
{
...
static Matrix3f RotationYXZ(const Vector3f &angles){...}
...
};
You can see it as a constructor actually but since we have different permutations of the rotations' axis priority, I can't think of a smart way to overload them, as the parameters are always 3 angles. Anyway Matrix3f::RotationYXZ() is better self-describing.
I should keep in mind that my programming teacher told me static functions are the ways the devil use to thrown us back to the dark-age of procedural programming. On the other hand if I'd have really listen to him I'd be programming on Eiffel instead of Cpp now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!