Upvoted by , Ph.D. Computer Science, Brown University (2005) and , PhD in Computer Science from USCAuthor has 1.3K answers and 5.2M answer views · 5y ·
You can always check it yourself.
Take the very simple code:
- int foo1(int x) {
- return x * 2;
- }
- int foo2(int x) {
- return x << 1;
- }
Compile it and see the result of clang generated optimized code, for x86_64:
- _foo1:
- 0000000000000000 pushq %rbp
- 0000000000000001 movq %rsp, %rbp
- 0000000000000004 leal _foo1(%rdi,%rdi), %eax
- 0000000000000007 popq %rbp
- 0000000000000008 retq
- 0000000000000009 nopl _foo1(%rax)
- _foo2:
- 0000000000000010 pushq %rbp
- 0000000000000011 movq %rsp, %rbp
- 0000000000000014 leal _foo1(%rdi,%rdi), %eax
- 0000000000000017 popq %rbp
- 0000000000000018 retq
The code of these two functions is 100% the same (disregard the “_foo1” symbol disassembler used to show zero offset, and the alignment no-op at address 0x0009)
So do not waste your time trying to be smarter than compiler. Just write clean and readable code.
64.9K views ·
View upvotes
· View 18 shares
· 1 of 22 answers
Something went wrong. Wait a moment and try again.