SFML community forums

Help => General => Topic started by: AlexAUT on January 07, 2014, 03:27:13 pm

Title: sf::Vector2f per value or const reference
Post by: AlexAUT on January 07, 2014, 03:27:13 pm
Hello,
Simple question, is it worth using a const reference as parameter in case of performance? Because in my opinion the const sf::Vector2f &xyz really disturb the reading flow.

Examples:
void setPosition(sf::Vector2f position);
void setPosition(const sf::Vector2f &position);

//And even worse
sf::Vector2f getPosition() const;
const sf::Vector2f &getPosition()const;
 



AlexAUT
Title: Re: sf::Vector2f per value or const reference
Post by: amir ramezani on January 07, 2014, 07:24:37 pm
don't use const that doesn't help performance so much
if you can't use it, you may ran into very very bad problems (run-time errors)
it doesn't
Title: Re: sf::Vector2f per value or const reference
Post by: AlexAUT on January 07, 2014, 07:59:50 pm
Ähm what? Runtime errors because  :o? I haven't problems with it.

But nvm, my question isnt about the const rather about the per reference vs per value.



AlexAUT
Title: Re: sf::Vector2f per value or const reference
Post by: amir ramezani on January 07, 2014, 08:16:46 pm
you can pass it as a reference!
why not?
Title: Re: sf::Vector2f per value or const reference
Post by: AlexAUT on January 07, 2014, 08:21:59 pm
The question is about performance. For big objects it's better to pass it by const reference compared to per value. (No copy of the object). But is it also worth for a small object like a sf::vector2f.


AlexAUT
Title: Re: sf::Vector2f per value or const reference
Post by: Nexus on January 07, 2014, 08:28:20 pm
For sf::Vector2f, the reference doesn't really pay off. Imagine a double, it has the same size as 2 floats, yet you pass it by value. Also, the reference isn't free either; it also needs to be copied, but even more importantly, dereferenced. You will hardly notice any performance difference, especially considering that compilers are also allowed to optimize such code.

In my own code, I pass sf::Vector2f by value, since it's more readable.

For return types, there is even another argument in favor of copies: It leaves more freedom to the implementation, while const-references require a member variable or another object that is still valid.
Title: Re: sf::Vector2f per value or const reference
Post by: AlexAUT on January 07, 2014, 08:58:21 pm
Thanks. Thats exactly the answer I was looking for  :D


AlexAUT
Title: Re: sf::Vector2f per value or const reference
Post by: Kojay on January 15, 2014, 12:30:07 pm
A bit late, but I haven't gotten around to making an example the assembly output of which I could examine, as I was hoping for. Nonetheless:

sf::Vector is not POD therefore copying it is not supposed to be the same as the bitwise copy of double.

Furthermore, if one's in the habit of always passing non built-ins by  reference, the code is not any less readable (on the contrary, passing by copy makes it stand out). And presumably the editor highlights const differently so the type still stands out.

edit 20/1:
a simple bechmark:

#include <SFML/System/Vector2.hpp>
#include <boost/timer/timer.hpp>
#include <vector>

void copy(sf::Vector2f vector) {
}

void reference(const sf::Vector2f& vector){
}


int main()
{

    std::vector<sf::Vector2f> vectors(1000000);

    {
        boost::timer::auto_cpu_timer t;
        for (const auto& vector : vectors)
            copy(vector);
    }



    {
        boost::timer::auto_cpu_timer t;
        for (const auto& vector : vectors)
            reference(vector);
    }

    return 0;
}
 

Compiled by g++ without optimization; passing by reference comes out 1ms faster on average.