Advertisement
Guest User

if versus switch (clang -O2)

a guest
Oct 5th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.40 KB | None | 0 0
  1. /* Compile with clang -O2 -S. Observe that both versions of the function have identical x86 assembly code. */
  2.  
  3. #include <stdio.h>
  4.  
  5. typedef enum { APPLE, BANANA, PEAR, ORANGE, PEACH, CHERRY } Fruit;
  6.  
  7. void with_switch(Fruit x)
  8. {
  9.     switch (x) {
  10.         case APPLE: case ORANGE: case BANANA: puts("ok");
  11.     }
  12. }
  13.  
  14. void with_if(Fruit x)
  15. {
  16.     if (x == APPLE || x == ORANGE || x == BANANA) puts("ok");
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement