Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Vector2f per value or const reference  (Read 5549 times)

0 Members and 1 Guest are viewing this topic.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
sf::Vector2f per value or const reference
« 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

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: sf::Vector2f per value or const reference
« Reply #1 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
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: sf::Vector2f per value or const reference
« Reply #2 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

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: sf::Vector2f per value or const reference
« Reply #3 on: January 07, 2014, 08:16:46 pm »
you can pass it as a reference!
why not?
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: sf::Vector2f per value or const reference
« Reply #4 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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Vector2f per value or const reference
« Reply #5 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: sf::Vector2f per value or const reference
« Reply #6 on: January 07, 2014, 08:58:21 pm »
Thanks. Thats exactly the answer I was looking for  :D


AlexAUT

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: sf::Vector2f per value or const reference
« Reply #7 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.
« Last Edit: January 21, 2014, 12:30:42 am by Kojay »