请问案件switch语句的顺序可以不同的表现? [英] Does the order of case in Switch statement can vary the performance?

查看:113
本文介绍了请问案件switch语句的顺序可以不同的表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说我有一个switch语句如下

Let say I have a switch statement as below

switch(alphabet) {

    case "f":
        //do something
        break;

    case "c":
        //do something
        break;

    case "a":
        //do something
        break;

    case "e":
        //do something
        break;

}

现在假设我知道,有频率字母 e的最高其次是A,C和分别F。所以,我只是调整了情况语句顺序,使他们如下:

Now suppose I know that the frequency of having Alphabet e is highest followed by a, c and f respectively. So, I just restructured the case statement order and made them as follows:

switch(alphabet) {

    case "e":
        //do something
        break;

    case "a":
        //do something
        break;

    case "c":
        //do something
        break;

    case "f":
        //do something
        break;
}



将在第二开关语句会比第一个开关语句快?如果是的话,如果在我的计划,我需要调用这个开关语句说了很多次,那将是一个很大的改进?或者,如果没有在任何我怎么可以用我的频率知识来提高性能?

Will the second switch statement be faster than the first switch statement? If yes and if in my program I need to call this switch statement say many times, will that be a substantial improvement? Or if not in any how can I use my frequency knowledge to improve the performance?

推荐答案

没那么多,你应该关注。这当然不是一件可以预测的。

Not so much that you should be concerned. It's certainly not something that can be predicted.

通过字符串大小写的标签,则编译器实际使用的字符串映射到指数在跳转表的内部哈希表。所以操作实际上是O(1) - 独立唱片公司的数量

With string case labels, the compiler actually uses an internal hash table that maps the strings to indexes in a jump-table. So the operation is actually O(1) - independent of the number of labels.

有关整数标签,那么我相信会产生实际的代码依赖于数量标签和有无号码是连续的(或几乎连续)。如果他们是连续的(1,2,3,4,...),那么他们就会被转化成一个跳转表。如果有很多人,那么哈希表+跳表(像串)将被使用。如果只有几个标签,他们不是表立即被改造成一个跳转表,的只有的将被改造成一系列if..then..else语句。

For integer labels, then I believe the actual code that is generated depends on the number of labels and whether the numbers are consecutive (or "almost" consecutive). If they're consecutive (1, 2, 3, 4, ...) then they'll just be transformed into a jump table. If there's a lot of them, then the Hashtable+jump table (like with strings) will be used. If there's only a few labels and they're not table to be immediately transformed into a jump table, only then will be transformed into a series of if..then..else statements.

在一般情况下,虽然,你应该写代码,这样的你可以阅读的,不是这样编译器能产生更快的代码。

In general, though, you should write code so that you can read it, not so that the compiler can produce "faster" code.

(注意我上面的描述是C#编译器的内部工作原理的实现细节:你不应该依赖于它的总是的那样工作 - 事实上,它甚至可能没有工作,完全一样的现在的,但至少它的总体思路)。

(Note my description above is an implementation detail of how the C# compiler works internally: you shouldn't rely on it always working like that -- in fact, it might not even work exactly like that now, but at least it's the general idea).

这篇关于请问案件switch语句的顺序可以不同的表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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