“自制” STL 的性能?

发布于 2024-07-15 18:33:44 字数 298 浏览 5 评论 0原文

http://www.open-std。 org/jtc1/sc22/wg21/docs/papers/2007/n2271.html

根据该文章,STL 不适合游戏开发。 您对此有何看法?

我目前的做法是这样的: 使用STL,如果会导致与自制容器(或分配器)交换的性能问题(还没有谈到它,但我不是在做高端3D游戏;))

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html

According to that article STL isn't suited for game-development.
What are your thoughts about this ?

My current approach is this :
use STL, if leads to performance problems exchange with homebrew container (or allocator) (didn't come to it yet, but I'm not doing a high-end 3d game ;) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

握住你手 2024-07-22 18:33:44

在这种情况下,您的方法是唯一明智的选择。 优化规则#1 是“除非您确切知道瓶颈在哪里,否则不要优化”。

稍后您仍然应该能够相对轻松地交换容器,特别是如果您使用通过 typedef 定义的类型而不是直接使用 STL 容器。 我的意思是:

#include <vector>

typedef std::vector<int> MyIntVectorType;

int main()
{
   MyIntVectorType theVector; 
}

Your approach is the only sane option in this case. Rule #1 for optimization is "do not optimize unless you know exactly where the bottlenecks are".

You should still be able to swap your container relatively easily later on, especially if you use a type defined via typedef instead of directly using the STL container. I mean something like:

#include <vector>

typedef std::vector<int> MyIntVectorType;

int main()
{
   MyIntVectorType theVector; 
}
阪姬 2024-07-22 18:33:44

您必须了解 STL 在幕后所做的事情。 例如,如果您使用向量,不要让它任意增长,请使用 vector::resize() 预先进行分配,这样它只分配一次。 像这样的东西。 你的方法还不错——只要做好功课就可以了。

You have to be aware of what STL is doing under the covers. For example, if you use a vector, for example, don't just let it grow arbitrarily, use vector::resize() to do the allocation up front so it only allocates once. Stuff like that. Your approach isn't bad—just do your homework.

洛阳烟雨空心柳 2024-07-22 18:33:44

如果您刚刚开始游戏编程,请继续使用 STL,它很棒而且很稳定。 你最好选择一些有效且稳定的东西,而不是为了一两帧每秒而开始抓挠。 然而,以下是许多大型工作室不使用它的两个原因。

  • 在某些时候,您会希望将系统推向极限。 如果您的引擎非常大,优化容器是获得无处不在(图形、音频、物理等)的好方法。 通过组装进行优化,为不同的用途提供不同的容器实现将成为必须。 模板并不是最精简的实现。
  • STL在不同平台(PS2、PS3、Wii、360、PC等)上有不同的实现。 如果您正在跨平台工作,有时这些差异会在系统的根源(所谓的“全平台代码”)中产生问题。 当这种情况发生在你身上一次时,你会想要转向一些你能够以更简单的方式维护的东西。

拥有自定义版本的容器并不是一项简单的工作,只有在值得的情况下才这样做。

If you are just starting with game programming, go on and use STL, it's great and it's stable. You are better off with something that works and is stable than to start scratching for a fps or two. However, here are the two reasons why many major studio don't use it.

  • At some point, you'll want to push your system to the limit. If your engine is very large, optimizing container is a good way to gain everywhere (graphic, audio, physics, etc.). Optimizing with assembly, having different implementations of a container for different use will become a must. Templates aren't the leanest implementation ever.
  • STL have a different implementation on different plateforms (PS2, PS3, Wii, 360, PC etc.). If you are working crossplatform, sometimes those differences will create problems at the very root of your system, in the supposed "all-plateform code". When this happened to you once, you will want to move to something that you will be able to maintain in an easier way.

Having a custom version of container isn't a trivial job, do it only if it's worth it.

蓝眸 2024-07-22 18:33:44

使用STL,如果它在可接受的性能范围内工作,则保留它。 如果没有,请尝试其他方法。 在尝试现有的实现之前,您不想重写已经存在的东西。

Use STL, and if it works within an acceptable performance range, keep it. If not, try something else. You don't want to be re-writing something that already exists before you try the existing implementation.

独留℉清风醉 2024-07-22 18:33:44

有时需要更换的不是容器,而是所包含的对象。 我曾经有一个很大的 std::map,其中有一个字符串作为键,而且速度太慢。 我意识到所有键的大小都相同,因此我用一个专门的版本替换了字符串类,该版本是我作为固定大小字符缓冲区的包装器编写的,并且性能不再是问题。

知道你的瓶颈在哪里,并且不要假设你可以编写任何东西的更快版本,除非你可以做出巨大的简化假设。

Sometimes it's not the container that needs replacing, it's the contained object. I once had a large std::map that had a string for a key, and it was too slow. I realized that all of my keys were the same size, so I replaced the string class with a specialized version that I wrote as a wrapper around a fixed-size char buffer, and performance was no longer an issue.

Know where your bottlenecks are, and don't assume you can write a faster version of anything unless there are huge simplifying assumptions you can make.

坏尐絯 2024-07-22 18:33:44

C++ 中许多与执行相关的问题已在 C++0x< /a>.

Many of the performed-related problems in C++ have been fixed in C++0x.

﹏雨一样淡蓝的深情 2024-07-22 18:33:44

您还可以将 STL 容器中的分配器替换为自定义分配器。

You can also swap the allocator in STL containers for a custom one.

冰火雁神 2024-07-22 18:33:44

您可能想查看类似 EASTL< /a>,专为游戏设计。

编辑:抱歉,我认为问题中的链接是另一篇文章。

You may want to look at something like EASTL, designed specifically for games.

Edit: Apologies, I thought the link in the question was a different article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文