许多情况下,交换机的优化保证了任何情况下的平等机会的时间? (C ++) [英] Switch optimization for many cases guarantees equal access time for any case? ( C++ )

查看:93
本文介绍了许多情况下,交换机的优化保证了任何情况下的平等机会的时间? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到的答案为特定的语言,用5例以上正与跳转表进行了优化,以保证恒定的访问时间任何情况下有关切换。结果
是这样的C / C ++?结果
它是特别为GCC?为Visual Studio?结果
如果没有,会在出现频率为了帮助排序情况?

I've seen answers here for specific languages, about switches with more than 5 cases being optimized with jump tables to guarantee constant access time for any case.
Is that so for C / C++?
Is it in particular for gcc? for visual studio?
If not, would sorting cases in order of occurrence frequency help?

推荐答案

该标准并不能保证对switch语句将如何实现任何事情。我从来没有见过一个编译器产生一个哈希表,虽然不少会产生一个跳转表。除非我的记忆工作比以往更糟糕,既VS和gcc可以产生跳转表当案件是足够致密的(为充分不同的值)。不幸的是,这几乎是不可能的说(或必然甚至搞清楚)按发生频率排序将帮助时 - 它不仅是编译器之间,但不同的版本相同的编译器甚至之间是不同的。

The standard doesn't guarantee anything about how the switch statement will be implemented. I've never seen a compiler produce a hash table, though quite a few will produce a jump table. Unless my memory is working even worse than usual, both VS and gcc can produce jump tables when the cases are sufficiently dense (for different values of "sufficiently"). Unfortunately, it's almost impossible to say (or necessarily even figure out) when sorting by frequency of occurrence will help -- it's different not only between compilers, but even between different versions of the same compiler.

这篇关于许多情况下,交换机的优化保证了任何情况下的平等机会的时间? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆