C++ 静态内部功能

C++ Static inside function

本文关键字:功能 内部 静态 C++      更新时间:2023-10-16

你好,我有一个返回 std::p air 的函数,并且经常被调用。

std::pair<sf::Vector2i, sf::Vector2i> 
Map::map_coord_to_chunk_coord(int x, int y) {
  // Get the chunk position
  int chunk_x = x / CHUNK_SIZE;
  int chunk_y = y / CHUNK_SIZE;
  // Get the position inside the chunk
  x = x - chunk_x * CHUNK_SIZE;
  y = y - chunk_y * CHUNK_SIZE;
  // Return the chunk position and the position inside it
      return std::pair<sf::Vector2i, sf::Vector2i>(sf::Vector2i(chunk_x, 
chunk_y), sf::Vector2i(x, y));
}

最好将货币对声明为静态,这样就不会每次都创建它。

std::pair<sf::Vector2i, sf::Vector2i> 
Map::map_coord_to_chunk_coord(int x, int y) {
  static std::pair<sf::Vector2i, sf::Vector2i> coords;
  // Get the chunk position
  coords.first.x = x / CHUNK_SIZE;
  coords.first.y = y / CHUNK_SIZE;
  // Get the position inside the chunk
  coords.second.x = x - coords.first.x * CHUNK_SIZE;
  coords.second.y = y - coords.first.y * CHUNK_SIZE;
  // Return the chunk position and the position inside it
  return coords;
}

我运行callgrind,看起来这个函数快了3倍,但这是一个很好的做法吗?

一般来说,当唯一目标是节省 CPU 周期时,应该避免使用 static

coords设置为静态会使map_coord_to_chunk_coord函数不可重入,这意味着如果没有正确同步,它将无法再在并发环境中使用。对于节省简单对象的建筑成本,这是一个非常高的代价。

例如,您应该能够通过使用make_pair来优化std::pair的构造:

inline std::pair<sf::Vector2i, sf::Vector2i> 
Map::map_coord_to_chunk_coord(int x, int y) {
    int first_x = x / CHUNK_SIZE;
    int first_y = y / CHUNK_SIZE;
    return std::make_pair(
        sf::Vector2i(first_x, first_y)
    ,   sf::Vector2i(x - first_x * CHUNK_SIZE, y - first_y * CHUNK_SIZE)
    );
}

在某些情况下,编译器甚至可以优化复制,进一步提高性能。

正如其他人指出的那样,您通常应该避免以这种方式使用局部静态变量,因为它会使代码非线程安全。

通常,最惯用C++是依赖返回值优化和其他编译器优化。如果std::pair sf::Vector2i的构造是代码中的瓶颈,我会感到惊讶(也有点羡慕(,但如果这真的是代码的关键部分,你可以稍微不那么习惯和按引用传递,而不是使用返回值:

void
map_coord_to_chunk_coord(int x, int y, std::pair<Vector2i, Vector2i>& chunk_coord) {
    chunk_coord.first.x = x / CHUNK_SIZE;
    chunk_coord.first.y = y / CHUNK_SIZE;
    chunk_coord.second.x = x % CHUNK_SIZE;
    chunk_coord.second.y = y % CHUNK_SIZE;
}

然后调用方可以在紧密循环中重用chunk_coord变量,并且您没有std::pairsf::Vector2i的构造。

No.

无论如何,你都必须复制它(按值返回!(,所以你什么也没得到。

(您实际上只添加了一个构造。

你实现的只是让你的函数不可重入,这是一个倒退。