C++ 静态内部功能
C++ Static inside function
你好,我有一个返回 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::pair
或sf::Vector2i
的构造。
No.
无论如何,你都必须复制它(按值返回!(,所以你什么也没得到。
(您实际上只添加了一个构造。
你实现的只是让你的函数不可重入,这是一个倒退。
相关文章:
- 对象内部对象的重载功能
- 案例内部的功能
- C 功能如何内部工作
- 如何在 OpenACC 中并行化内部具有"min"功能的循环
- 从内部功能中删除Smart_PTR
- C++ 静态内部功能
- 套接字连接在主功能内部但不在功能内部
- 如何创建功能以用受保护或私人侵犯的对象在对象内部操纵对象
- 如何将功能指针传递到该功能内部的函数
- 当指针作为指向另一个功能内部指针的指针传递时会发生什么
- IFDEF块内部对功能的未定义引用
- 具有模板类型内部功能的不同操作的NOExcept
- C 如何将数组传递到功能内部功能
- 在外部功能中具有一个变量初始化或内部功能中的多个初始化是最有效的
- C 常数和骨料内部功能主体与外部
- 访问主功能内部的私有数据
- 使用合格类型实例化的模板内部功能的名称分辨率
- 使用内部删除功能
- 与朋友在课堂上进行内部功能,以隐藏公共细节
- 在开关状态情况下,模板功能内部的不同类型的分配