案件数量庞大的switch语句 [英] Switch statement with huge number of cases

查看:73
本文介绍了案件数量庞大的switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在开关已超过5000 情况下,会发生什么。哪些缺点,以及我们如何可以用更快的东西代替它?

What happens if the switch has more than 5000 case. What are the drawbacks and how we can replace it with something faster?

请注意:我不希望使用数组来存储的情况下,因为它是在同一个

Note: I am not expecting to use array to store cases as it's the same.

推荐答案

有没有具体的理由认为你会想超过一个开关/ case语句以外的任何其他(实际上我积极地希望它是无用的)。编译器应该建立高效的调度code,这可能涉及静态[疏]表(S)和直接索引,二进制分支等的组合;它有见解的情况下,静态值和应该做一个出色的工作(每次更改的情况下的时间重新调整它的飞行,而没有用手工制作的方式合身新的价值 - 比如大相径庭的价值观当你不得不打包数组查找一个pretty - 可能需要code的返工或默默导致内存膨胀或性能下降)

There's no specific reason to think you'd want anything other than a switch/case statement (and indeed I'd actively expect it to be unhelpful). The compiler should create efficient dispatching code, which might involve some combination of static [sparse] table(s) and direct indexing, binary branching etc.; it's got insights into the static values of the cases and should do an excellent job (retuning it on the fly each time you change the cases, whereas new values that don't fit well with a hand-crafted approach - such as wildly differing values when you'd had a pretty packed array lookup - could require reworking of code or silently cause memory bloat or a performance drop).

人们真正关心这种东西回来时,下尝试...编译器被关押创造良好的code负责拉拢硬核组装的程序员。换句话说 - 如果不是(可测量)破,不解决它。

People really cared about this kind of thing back when C was trying to win over hard-core assembly programmers... the compilers were held accountable for generating good code. Put another way - if it's not (measurably) broken, don't fix it.

更一般地,这是伟大的,是好奇这样的事情,让人们对替代品的想法和他们的性能影响,但如果你的真正的关心和性能上的差异可以使一个有用的区别你程序(尤其是如果分析表明它),然后总是用基准程序做实际工作。

More generally, it's great to be curious about this kind of thing and get people's ideas on alternatives and their performance implications, but if you really care and the performance difference could make a useful difference to your program (especially if profiling suggests it) then always benchmark with your program doing real work.

这篇关于案件数量庞大的switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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