linux c 效率高,if和switch效率的再研究 - linux c - ubuntuer zone

你把 case 106 修改为case 119,你就会发现查找表没有你想象中的大,好像这种优化是不确定的》

我把它改成 case 119 (其余不变)后,IDA的反汇编结果:

.text:00401020 switch_eg       proc near               ; CODE XREF: j_switch_egj

.text:00401020

.text:00401020 var_48          = dword ptr -48h

.text:00401020 var_8           = dword ptr -8

.text:00401020 var_4           = dword ptr -4

.text:00401020 arg_0           = dword ptr  8

.text:00401020

.text:00401020                 push    ebp

.text:00401021                 mov     ebp, esp

.text:00401023                 sub     esp, 48h

.text:00401026                 push    ebx

.text:00401027                 push    esi

.text:00401028                 push    edi

.text:00401029                 lea     edi, [ebp+var_48]

.text:0040102C                 mov     ecx, 12h

.text:00401031                 mov     eax, 0CCCCCCCCh

.text:00401036                 rep stosd

.text:00401038                 mov     eax, [ebp+arg_0]

.text:0040103B                 mov     [ebp+var_4], eax

.text:0040103E                 mov     ecx, [ebp+arg_0]

.text:00401041                 mov     [ebp+var_8], ecx

.text:00401044                 mov     edx, [ebp+var_8]

.text:00401047                 sub     edx, 64h

.text:0040104A                 mov     [ebp+var_8], edx

.text:0040104D                 cmp     [ebp+var_8], 13h

.text:00401051                 ja      short loc_401090

.text:00401053                 mov     ecx, [ebp+var_8]

.text:00401056                 xor     eax, eax

.text:00401058                 mov     al, ds:byte_4010B5[ecx]

.text:0040105E                 jmp     ds:off_4010A1[eax*4]

.text:00401065

.text:00401065 loc_401065:                             ; DATA XREF: .text:off_4010A1o

.text:00401065                 mov     edx, [ebp+var_4]

.text:00401068                 imul    edx, 0Dh

.text:0040106B                 mov     [ebp+var_4], edx

.text:0040106E                 jmp     short loc_401097

.text:00401070 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?

.text:00401070

.text:00401070 loc_401070:                             ; CODE XREF: switch_eg+3Ej

.text:00401070                                         ; DATA XREF: .text:004010A5o

.text:00401070                 mov     eax, [ebp+var_4]

.text:00401073                 add     eax, 0Ah

.text:00401076                 mov     [ebp+var_4], eax

.text:00401079

.text:00401079 loc_401079:                             ; CODE XREF: switch_eg+3Ej

.text:00401079                                         ; DATA XREF: .text:004010A9o

.text:00401079                 mov     ecx, [ebp+var_4]

.text:0040107C                 add     ecx, 0Bh

.text:0040107F                 mov     [ebp+var_4], ecx

.text:00401082                 jmp     short loc_401097

.text:00401084 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?

.text:00401084

.text:00401084 loc_401084:                             ; CODE XREF: switch_eg+3Ej

.text:00401084                                         ; DATA XREF: .text:004010ADo

.text:00401084                 mov     edx, [ebp+var_4]

.text:00401087                 imul    edx, [ebp+var_4]

.text:0040108B                 mov     [ebp+var_4], edx

.text:0040108E                 jmp     short loc_401097

.text:00401090 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?

.text:00401090

.text:00401090 loc_401090:                             ; CODE XREF: switch_eg+31j

.text:00401090                                         ; switch_eg+3Ej

.text:00401090                                         ; DATA XREF: ...

.text:00401090                 mov     [ebp+var_4], 0

.text:00401097

.text:00401097 loc_401097:                             ; CODE XREF: switch_eg+4Ej

.text:00401097                                         ; switch_eg+62j ...

.text:00401097                 mov     eax, [ebp+var_4]

.text:0040109A                 pop     edi

.text:0040109B                 pop     esi

.text:0040109C                 pop     ebx

.text:0040109D                 mov     esp, ebp

.text:0040109F                 pop     ebp

.text:004010A0                 retn

.text:004010A0 switch_eg       endp

.text:004010A0

.text:004010A0 ; 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?

.text:004010A1 off_4010A1      dd offset loc_401065    ; DATA XREF: switch_eg+3Er

.text:004010A5                 dd offset loc_401070

.text:004010A9                 dd offset loc_401079

.text:004010AD                 dd offset loc_401084

.text:004010B1                 dd offset loc_401090

.text:004010B5 byte_4010B5     db 0                    ; DATA XREF: switch_eg+38r

.text:004010B6                 dw 104h

.text:004010B8                 dd 4040302h, 3 dup(4040404h), 0CCCCCC03h, 0Dh dup(0CCCCCCCCh)

.text:00401100

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值