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
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.