“自制” STL 的性能?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在这种情况下,您的方法是唯一明智的选择。 优化规则#1 是“除非您确切知道瓶颈在哪里,否则不要优化”。
稍后您仍然应该能够相对轻松地交换容器,特别是如果您使用通过 typedef 定义的类型而不是直接使用 STL 容器。 我的意思是:
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:
您必须了解 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.
如果您刚刚开始游戏编程,请继续使用 STL,它很棒而且很稳定。 你最好选择一些有效且稳定的东西,而不是为了一两帧每秒而开始抓挠。 然而,以下是许多大型工作室不使用它的两个原因。
拥有自定义版本的容器并不是一项简单的工作,只有在值得的情况下才这样做。
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.
Having a custom version of container isn't a trivial job, do it only if it's worth it.
使用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.
有时需要更换的不是容器,而是所包含的对象。 我曾经有一个很大的 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.
C++ 中许多与执行相关的问题已在 C++0x< /a>.
Many of the performed-related problems in C++ have been fixed in C++0x.
您还可以将 STL 容器中的分配器替换为自定义分配器。
You can also swap the allocator in STL containers for a custom one.
您可能想查看类似 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.