Hello, I have a function that returns std :: pair and is called very often.
std::pair<sf::Vector2i, sf::Vector2i>
Map::map_coord_to_chunk_coord(int x, int y) {
int chunk_x = x / CHUNK_SIZE;
int chunk_y = y / CHUNK_SIZE;
x = x - chunk_x * CHUNK_SIZE;
y = y - chunk_y * CHUNK_SIZE;
return std::pair<sf::Vector2i, sf::Vector2i>(sf::Vector2i(chunk_x,
chunk_y), sf::Vector2i(x, y));
}
Is it better to declare a pair as static so that it is not created every time.
std::pair<sf::Vector2i, sf::Vector2i>
Map::map_coord_to_chunk_coord(int x, int y) {
static std::pair<sf::Vector2i, sf::Vector2i> coords;
coords.first.x = x / CHUNK_SIZE;
coords.first.y = y / CHUNK_SIZE;
coords.second.x = x - coords.first.x * CHUNK_SIZE;
coords.second.y = y - coords.first.y * CHUNK_SIZE;
return coords;
}
I run callgrind, and it looks like this function works 3 times faster, but is this good practice?
source
share