在程序代码中切换和if..else相同? [英] switch and if..else same at assemby code ?

查看:99
本文介绍了在程序代码中切换和if..else相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是if..else和switch语句类似的汇编代码。我想b / b
想知道switch是否也为每个案例使用价值比较

内部或者它是什么

直接在汇编时跳转到案例对于性能关键型应用程序来说,
是否更好地使用开关

case或

使用fn指针完成同样的操作?

请咨询。


感谢您的时间。

prakash

解决方案

Hello

是if..else和switch语句类似的汇编代码。


不是所有的时间。取决于编译器和开关值。

我想知道switch是否也在内部对每个案例使用值比较

直接跳转到案例装配水平?


在大多数情况下,编译器会生成一个切换跳转表。

基本上由两个表组成:值表和代码偏移表。

这比if / else语句更快。


如果您调试自己的示例程序
使用switch语句的
,您将更好地理解这一点。


但是,如果开关值不是某种顺序,编译器

可能生成一个ASM代码,看起来像一个系列if / else。 br />

我指的是我在Intel x86

机器上由C编译器生成的代码。

表示性能关键应用程序是否更好地使用switch
case或
使用fn指针完成相同的操作?


即使您使用函数指针,也必须编写一个代码,将给定值的
a映射到函数指针表中的给定索引。 />
不知何故类似于编译器生成的代码。

请建议。




你最好问这个在comp.lang.asm.x86或alt.lang.asm中再次提出问题


希望有所帮助,

Elias

< br>

在文章< 7b ************************* @ posting.google.com> ,

hasadh< h _ **** @ hotmail.com>写道:

是if..else和switch语句类似的汇编代码。我想知道switch是否也在内部使用每个案例的值比较
它是否直接在汇编级别跳转到案例?


这取决于实施。如果所有(或大多数)值都是连续的,它很可能会编译为跳转

表,并且如果它们不是,则使用条件

。即使跳转表也可能需要将

控制变量与值范围进行比较,除非您使用该类型允许的所有值

(例如256个开启的情况) 8位字符。

对于性能关键的应用程序来说,最好是使用switch
case还是使用fn指针完成相同的操作?




同样,这取决于你的实现和你在做什么。如果

你没有很多状态可以传递,函数指针是非常自然的,但对于类似虚拟机的东西,它会经常

使用大开关更快,或者可能是非标准扩展(特别是

,为变量分配标签的能力)。


- Richard


hasadh写道:



是if的汇编代码。 else和switch语句类似。我想知道开关是否也在内部使用每种情况的值比较

直接在装配级跳转到外壳?

性能关键应用是否更好地使用开关
情况或使用fn指针完成相同的工作?
请指教。

感谢您的时间。
prakash




生成巨大的跳转表之间存在平衡

或在实现时生成太多的if / else结构
$汇编代码中的b $ ba开关语句。


在lcc-win32编译器中,这由密度确定。参数,

以默认值0.5开头。


用户可以使用

#pragma密度控制此参数

构造。


这允许你强制编译器生成

跳转表,即使切换案例值不是'''连续的。在

a应用程序的时间关键位置,您可以通过强制编译器使用

#pragma密度构建跳转表来交换空间速度

(0)。


如果你想减少数据空间的使用,可以通过设置
转换语句转换为一系列if / else语句/>
#pragma密度(1.0)


参考资料

----------

Fraser和Hanson一个可重定向的C编译器
http://www.amazon.com/exec/obidos/tg...glance&s=books


Hi,

is the assemly code for if..else and switch statements similar. I
would like to know if switch also uses value comparison for each case
internally or does it
jump to the case directly at the assembly level ?

for a performance critical application is it better to to use switch
case or
accomplish the same using fn pointers ?
please advice.

Thanks for your time.
prakash

解决方案

Hello

is the assemly code for if..else and switch statements similar.
Not all the time. Depends on the compiler and the switch values.
I would like to know if switch also uses value comparison for each case
internally or does it
jump to the case directly at the assembly level ?
In most cases, the compiler generates a switch jump table.
Basically comprised of two tables: values table and code offset table.
That is faster than if/else statments.

You will better understand this if you take debug your own sample program
that uses switch statement.

However, if the switch values are not in some sort of sequence the compiler
might generate an ASM code that looks like a serie if/else.

I am referring to code I''ve seen generated by C compilers on Intel x86
machines.
for a performance critical application is it better to to use switch
case or
accomplish the same using fn pointers ?
Even if you use function pointers, you would have to write a code that maps
a given value to a given index in the function pointers table.
Somehow similar to compiler''s generated code.
please advice.



You''ld better ask this question again in comp.lang.asm.x86 or alt.lang.asm

Hope that helps,
Elias


In article <7b*************************@posting.google.com> ,
hasadh <h_****@hotmail.com> wrote:

is the assemly code for if..else and switch statements similar. I
would like to know if switch also uses value comparison for each case
internally or does it jump to the case directly at the assembly level ?
It depends on the implementation. Quite likely it will compile to a jump
table if all (or most) of the values are contiguous, and use conditionals
if they aren''t. Even a jump table will probably have to compare the
control variable against the range of values, unless you use all the values
allowed by the type (e.g. 256 cases switching on an 8-bit char).
for a performance critical application is it better to to use switch
case or accomplish the same using fn pointers ?



Again, it depends on your implementation and what you''re doing. If
you don''t have lots of state to pass around, function pointers are
quite natural, but for something like a virtual machine it will often
be faster to use a big switch, or perhaps a non-standard extension (in
particular, the ability to assign labels to a variable).

-- Richard


hasadh wrote:

Hi,

is the assemly code for if..else and switch statements similar. I
would like to know if switch also uses value comparison for each case
internally or does it
jump to the case directly at the assembly level ?

for a performance critical application is it better to to use switch
case or
accomplish the same using fn pointers ?
please advice.

Thanks for your time.
prakash



There is a balance between generating huge jump tables
or generating too many if/else constructs when implementing
a switch statement in assembly code.

In the lcc-win32 compiler this is determined by a "density" parameter,
that starts with a default value of 0.5.

The user can control this parameter using a
#pragma density
construct.

This allows you to force the compiler to generate
jump tables even if the switch case values aren''t contiguous. In
a time critical place of the application you can trade space for speed
by forcing the compiler to build a jump table using
#pragma density(0).

If you want to minimize data space usage you can turn all
switch statements into a series of if/else statements by setting
#pragma density(1.0)

References
----------
Fraser and Hanson A retargetable C compiler
http://www.amazon.com/exec/obidos/tg...glance&s=books


这篇关于在程序代码中切换和if..else相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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