switch case 为何比if else 高效



If value issequential, it uses table to record the offset and jump to the executingaddress

If value is randomand big interval, it uses binary tree search and jump to the executing address

 

1.value is sequential

C source code:

 

Assembly language  :

 

static u8 flag=1;

    switch (flag)

73D5                       ld_s        r13,[pcl,0x1CC]   ; r13,[r63,460.]==[0xFCE4]

198D                       ldb_s       r0,[r13,0x19]   ;  à  loadflag value to r0

61B8                       sub_s       r0,r0,0x1; àr0=r0-1 get the index value because the rangof  flag value is from 1 to 16, complieroptimize code to index for offset ( for example: flag value is 1, offset is 0 )

2F083504                   brhs.d      r0,0x10,0xFB4E   ; à if r0 is higher or same as 16, break the switch directly because thebiggest offset is 15

A1C1                       sub_s       sp,sp,0x4    ; r28,r28,4.

40278173                   add         r1,pcl,0x0E   ; r1,r63,14. à  get case table address(FB32) for r1

33210000                    ldb.x       r0,[r1,r0] à  load the offset value  to r0

1479                       add1_s      r1,r1,r0 à  add theoffset  to r1: FB32+(r1<<1)

0079                       j_s         [r1] à  jump tor1

// table offset

08                         .byte       0x8 à case 1, go to 0xFB32+(8>>1)= 0xFE42

0A                         .byte       0x0A à  case 2,go to 0xFB32+(A>>1)= 0xFE46

08                         .byte       0x8à case 3, go to 0xFB32+(8>>1)= 0xFE42

0C                         .byte       0x0C à case 4, go to 0xFB32+(C>>1)= 0xFE4A

08                         .byte       0x8

0A                         .byte       0x0A

08                         .byte       0x8

0E                         .byte       0x0E

08                         .byte       0x8

0A                         .byte       0x0A

08                         .byte       0x8

0C                         .byte       0x0C

08                         .byte       0x8

0A                         .byte       0x0A

08                          .byte       0x8

0C                         .byte       0x0C


 

2.value is random and big interval

C source code:

 

Assembly language  :

 

Binary tree


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  switch (flag)

71D5                       ld_s        r13,[pcl,0x1C4]   ; r13,[r63,452.]

198D                       ldb_s       r0,[r13,0x19]   ; r0,[r13,25.] . à load flag value to r0

95E0                       cmp_s       r0,0x15      ; r0,21. àcompare r0 with 21: r0-21

A1C1                       sub_s       sp,sp,0x4    ; r28,r28,4.

12F2                       beq_s       0xFB44 à if r0=21, jump to 0xFB44

06F6                       bgt_s       0xFB30 à if r0>21, jump to 0xFB30, to compare r0with 65

1D085000                    breq        r0,0x1,0xFB40 à if r0=1, jump to 0xFB40

1508D001                   breq        r0,0x7,0xFB3C  àif r0=7, jump to 0xFB3C

0EF0                       b_s         0xFB48

C1E0                       cmp_s       r0,0x41      ; r0,65. à compare r0 with 21: r0-65

08F2                       beq_s       0xFB40  à if r0=65, jump to 0xFB40

E5E0                       cmp_s       r0,0x65      ; r0,101.

08F2                       beq_s       0xFB44

FEE0                       cmp_s       r0,0x7E      ; r0,126.

08F4                       bne_s       0xFB48


 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值