|
发表于 2021-8-16 18:16:42
|
显示全部楼层
优化问题看汇编即可 ,gcc下明显1 更好
- void fun1(unsigned int MemSize)
- {
- MemSize = (unsigned int)(((MemSize + (sizeof(HY_ALIGN_TYPE))) - ((HY_ALIGN_TYPE) 1)) / (sizeof(HY_ALIGN_TYPE))) * (sizeof(HY_ALIGN_TYPE));
- printf("%d", MemSize);
- }
-
- void fun2(unsigned int MemSize)
- {
-
- while(( MemSize % (sizeof(HY_ALIGN_TYPE)) != 0))
- MemSize ++;
- printf("%d", MemSize);
- }
复制代码
O2优化结果
- fun1:
- @ Function supports interworking.
- @ args = 0, pretend = 0, frame = 0
- @ frame_needed = 0, uses_anonymous_args = 0
- push {r4, lr}
- add r1, r0, #3
- bic r1, r1, #3
- ldr r0, .L4
- bl printf
- pop {r4, lr}
- bx lr
- .L5:
- .align 2
- .L4:
- .word .LC0
- .size fun1, .-fun1
- .align 2
- .global fun2
- .syntax unified
- .arm
- .fpu softvfp
- .type fun2, %function
- fun2:
- @ Function supports interworking.
- @ args = 0, pretend = 0, frame = 0
- @ frame_needed = 0, uses_anonymous_args = 0
- tst r0, #3
- push {r4, lr}
- mov r1, r0
- beq .L7
- .L8:
- add r1, r1, #1
- tst r1, #3
- bne .L8
- .L7:
- ldr r0, .L14
- bl printf
- pop {r4, lr}
- bx lr
- .L15:
- .align 2
- .L14:
- .word .LC0
- .size fun2, .-fun2
- .section .rodata.str1.4,"aMS",%progbits,1
- .align 2
- .LC0:
- .ascii "%d\000"
- .ident "GCC: (GNU Tools for Arm Embedded Processors 7-2017-q4-major) 7.2.1 20170904 (release) [ARM/embedded-7-branch revision 255204]"
复制代码 |
|