交替切换案例陈述 [英] Alternate to switch case statement

查看:78
本文介绍了交替切换案例陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这是iam第一次发帖,如果我犯了任何

错误。我的问题是iam使用switch case语句,其中
有大约100个case语句可供比较。所以只是好奇地发现

out是否有效使用这种方法?或者是否存在任何其他

替代方法,以便执行时间和代码大小可以减少



谢谢提前。


问候,

Satya

Hi everyone,

This is the first time iam posting excuse me if iam making any
mistake. My question is iam using a switch case statement in which i
have around 100 case statements to compare. so just curious to find
out is it effective to use this method?? or is there is any other
alternative method present so that execution time and code size can be
reduced??

Thanks in advance.

Regards,
Satya

推荐答案

>这个如果iam制作任何
>This is the first time iam posting excuse me if iam making any

>错误,那么iam第一次发帖请原谅。我的问题是iam使用switch case语句,其中我有大约100个案例陈述要比较。所以只是好奇地发现
是否有效使用这种方法?或者是否存在任何其他替代方法,以便可以减少执行时间和代码大小?
>mistake. My question is iam using a switch case statement in which i
have around 100 case statements to compare. so just curious to find
out is it effective to use this method?? or is there is any other
alternative method present so that execution time and code size can be
reduced??



下定决心:你想减少哪个,执行时间或代码

大小?通常,你会以牺牲另一个为代价来减少一个。


在很多情况下,编译器可能会使用二进制搜索方法

这相当于一个大嵌套的if / else。最多可以进行7次比较,这可能是
。或者,如果案例值是连续或接近这样的
,它可以使用一个分支表,这可能相当于大约3次比较的执行时间./ br
。 />

编译器不知道它将遇到的案例值

的相对频率。如果你这样做,你可能会通过首先检查最常见的值来平均击败它的代码

。你的代码将是b / b
凌乱,你应该留下关于你是什么的详细说明

优化,如果有人必须添加或删除案例。


9月11日,8:18 * am,Satya< mailtodsre ... @ gmail.comwrote:
On Sep 11, 8:18*am, Satya <mailtodsre...@gmail.comwrote:

大家好,


这是iam第一次发帖,如果我犯了任何

错误。我的问题是iam使用switch case语句,其中
有大约100个case语句可供比较。所以只是好奇地发现

out是否有效使用这种方法?或者是否存在任何其他

替代方法,以便执行时间和代码大小可以减少



谢谢提前。


问候,

Satya
Hi everyone,

This is the first time iam posting excuse me if iam making any
mistake. My question is iam using a switch case statement in which i
have around 100 case statements to compare. so just curious to find
out is it effective to use this method?? or is there is any other
alternative method present so that execution time and code size can be
reduced??

Thanks in advance.

Regards,
Satya



我认为是最快执行的最佳选择将是一个

指针数组,指向每个案例的函数。

然后你可以索引到这个数组,从而调用相应的

直接运行。


Kaustubh

I think the best option for fastest execution would be to make an
array of pointers pointing to functions for each case.
Your can then index into this array and thus call the corresponding
function directly.

Kaustubh


> iam使用switch case语句,其中i
>iam using a switch case statement in which i

有大约100个案例陈述要比较。
have around 100 case statements to compare.



这意味着您有100个常数值可与一个

变量进行比较。在这种情况下,如果可能的话,if else语句本身会被转换为转换语句。但是应该从

编译器组中确认。 comp.lang.gcc / g ++是两个选项


此外,所有条件都是独家的,即你是否在所有100个案例中放置

break语句或那里还有一些是通过

来实现的。

在这两种情况下,以更好的方式组织代码是好的。

你自己。 />

例如以下代码


switch(i){

案例1:/ *某些代码* / break ;

案例2:/ *一些代码* / break;

案例3:/ *一些代码* / breask;

案例4: / *一些代码* / breask;

..

..

..

..

案例100:/ *一些代码* / breask;

}


可以分成

以下树比较少。


if(i< 10){

switch(i){

case 1 :

案例2:

..

..

}


}否则if(i< 30){

switch(i){

case 10:

case 11:

..

..

案例29:

}

}否则如果(我< 50){

}否则if(i <70){

}否则{

switch(i){

案例70:

案例71:

..

..

案例100:

}

}


检查编译器是否自动为您执行此操作。你可以用

两种方式来做

1.去一些编译器组中询问comp.lang.gcc

2.检查程序集在这种情况下使用gcc -S选项生成的代码


- vIpIn


it means that you have 100 constant values to compare against one
variable. In such cases, if else statement itself are converted
to switch statements if possible. but it should be confirmed from the
compiler group. comp.lang.gcc/g++ are two options

Also, are all the conditions exclusive i.e. are you putting
break statement in all the 100 cases or there are some fall through
also.
in both the cases it will be good to organize the code in a better way
yourself.

for example the following code

switch(i){
case 1: /* some code */ break;
case 2: /* some code */ break;
case 3: /* some code */ breask;
case 4: /* some code */ breask;
..
..
..
..
case 100: /* some code */ breask;
}

can be broken into
following tree to have less comparisons.

if(i <10){
switch(i){
case 1:
case 2:
..
..
}

}else if (i < 30){
switch(i){
case 10:
case 11:
..
..
case 29:
}
}else if (i < 50){
}else if (i<70){
}else{
switch(i){
case 70:
case 71:
..
..
case 100:
}
}

check if compiler does that automatically for you. you can do it in
two ways
1. go and ask in some compiler group comp.lang.gcc
2. check the assembly code generated in such case using gcc -S option

-- vIpIn


这篇关于交替切换案例陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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