title: Does the order of cases in a switch statement affect performance?
body: <p>I have a <code>switch</code> case program:</p>
  
  <p><strong>Ascending order switch cases :</strong></p>
  
  <pre><code>int main()
  {
          int a, sc = 1;
          switch (sc)
          {
                  case 1:
                          a = 1;
                          break;
                  case 2:
                          a = 2;
                          break;
          }
  }
  </code></pre>
  
  <p>Assembly of code:</p>
  
  <pre><code>main:
          push    rbp
          mov     rbp, rsp
          mov     DWORD PTR [rbp-4], 1
          mov     eax, DWORD PTR [rbp-4]
          cmp     eax, 1
          je      .L3
          cmp     eax, 2
          je      .L4
          jmp     .L2
  .L3:
          mov     DWORD PTR [rbp-8], 1
          jmp     .L2
  .L4:
          mov     DWORD PTR [rbp-8], 2
          nop
  .L2:
          mov     eax, 0
          pop     rbp
          ret
  </code></pre>
  
  <p><strong>Descending order switch cases:</strong></p>
  
  <pre><code>int main()
  {
          int a, sc = 1;
          switch (sc)
          {
                  case 2:
                          a = 1;
                          break;
                  case 1:
                          a = 2;
                          break;
          }
  }
  </code></pre>
  
  <p>Assembly of code:</p>
  
  <pre><code>main:
          push    rbp
          mov     rbp, rsp
          mov     DWORD PTR [rbp-4], 1
          mov     eax, DWORD PTR [rbp-4]
          cmp     eax, 1
          je      .L3
          cmp     eax, 2
          jne     .L2
          mov     DWORD PTR [rbp-8], 1
          jmp     .L2
  .L3:
          mov     DWORD PTR [rbp-8], 2
          nop
  .L2:
          mov     eax, 0
          pop     rbp
          ret
  </code></pre>
  
  <p>Here, <strong>ascending</strong> order cases generated more assembly than <strong>descending</strong> order.</p>
  
  <p>So, <strong>if I have more numbers of switch cases, then does the order of cases affect performance?</strong> </p>