C++ - 提升获取问题

发布于 2024-09-28 14:42:27 字数 634 浏览 0 评论 0原文

有人知道 boost::variantboost::get性能消耗操作还是不是。

现在,我正在性能关键部分重构一些旧代码,其中“变体”是由容器为每种可能的类型和相应的枚举实现的。

显然,这很快,但很难看,现在当我必须重构代码以便它可以与另一种类型一起使用时,我想删除旧的代码部分并将其替换为boost::variant

此外,我不能简单地“分析两个变体并进行比较”,因为这种重构非常痛苦并且非常耗时。

因此,如果有人知道如何boost:: get 与通用的基于枚举的类型调度进行比较,如果您分享这些知识,我将不胜感激。

还有另一种将 boost::variant 与自定义访问者一起使用的变体(如 boost::variant 文档中所述) - 这可能比 boost::variant 更快code>boost::get 就我而言?

谢谢。

Does someone know if the boost::get for the boost::variant is a performance-consuming operation or not.

Right now I am refactoring some old code in a performance-critical part, where "varianting" was implementing by containers for each possible types and corresponding enum.

Obviously, this is fast, but ugly and right now when I have to refactor the code so that it would work with one more type, I want to get rid of that old part of code and replace it with boost::variant.

Also, I can't simple "profile both variants and compare" because this refactoring is a pain in the ass and would be pretty time consuming.

So, if someone knows how does boost::get<x> performs comparing to generic enum-based type dispatching, I would appreciate if you share this knowledge.

There is another variant of using boost::variant<types> with custom visitor (as described in boost::variant documentation) - may this be faster than boost::get in my case?

Thank you.

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

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

发布评论

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

评论(2

旧情勿念 2024-10-05 14:42:27

您仍然可以编写一个简单的测试应用程序来比较两者,它不必是生产环境。

我的一位同事最近也遇到了与此类似的问题。在他的场景中存在不同类型的对象,但他总是事先知道他期望哪种类型。而且他的数据结构很大,所以内存是一个问题。他通过使用void *reinterpret_cast解决了这个问题。这可以防止多态性的内存开销,并且速度非常快。但你必须绝对确定自己在做什么,否则事情就会爆炸。

You could still write a simple test-application to compare the two, it doesn't have to be the production environment.

A colleague of mine had a similar problem to this one recently. In his scenario there where objects of different types, but he always knew beforehand which type he expected. Also his data-structure was huge, so memory was an issue. He solved the problem by using void * and reinterpret_cast. This prevents the memory-overhead of polymorphism and is very fast. You have to be absolutely sure of what you are doing though, otherwise things will explode.

悲念泪 2024-10-05 14:42:27

看代码,get<>是使用boost::variant的内部访问者机制实现的。反过来,访问者机制依赖于mpl序列,并且是一步步执行的。这意味着在 n 类型变体上最多执行 n 步,但循环(递归调用)是存在的。同样,正如太空牛仔所建议的那样,进行一次小型性能测试会很有帮助。

Looking at the code, get<> is implemented using the internal visitor mechanism of the boost::variant. In turn, the visitor mechanism relies on mpl sequences, and it is executed step by step. That means at most n steps on an n type variant, but the loop (recursive call) is there. Again, as Space Cowboy suggests, a small performance test would be helpful.

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