title: In C, why is "signed int" faster than "unsigned int"?
body: <p>In C, why is <code>signed int</code> faster than <code>unsigned int</code>? True, I know that this has been asked and answered multiple times on this website (links below). However, most people said that there is no difference. I have written code and accidentally found a significant performance difference.</p>
  
  <p>Why would the "unsigned" version of my code be slower than the "signed" version (even when testing the same number)? (I have a x86-64 Intel processor).</p>
  
  <p><strong>Similar links</strong></p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8792162/faster-comparing-signed-than-unsigned-ints">Faster comparing signed than unsigned ints</a></li>
  <li><a href="https://stackoverflow.com/questions/4712315/performance-of-unsigned-vs-signed-integers">performance of unsigned vs signed integers</a></li>
  </ul>
  
  <p><strong>Compile Command:</strong> <code>gcc -Wall -Wextra -pedantic -O3 -Wl,-O3 -g0 -ggdb0 -s -fwhole-program -funroll-loops -pthread -pipe -ffunction-sections -fdata-sections -std=c11 -o ./test ./test.c &amp;&amp; strip --strip-all --strip-unneeded --remove-section=.note --remove-section=.comment ./test</code></p>
  
  <hr>
  
  <h3><code>signed int</code> version</h3>
  
  <p><strong>NOTE:</strong> There is no difference if I explicitly declare <code>signed int</code> on all numbers.</p>
  
  <pre><code>int isprime(int num) {
      // Test if a signed int is prime
      int i;
      if (num % 2 == 0 || num % 3 == 0)
          return 0;
      else if (num % 5 == 0 || num % 7 == 0)
          return 0;
      else {
          for (i = 11; i &lt; num; i += 2) {
              if (num % i == 0) {
                  if (i != num)
                      return 0;
                  else
                      return 1;
              }
          }
      }
      return 1;
  }
  </code></pre>
  
  <h3><code>unsigned int</code> version</h3>
  
  <pre><code>int isunsignedprime(unsigned int num) {
      // Test if an unsigned int is prime
      unsigned int i;
      if (num % (unsigned int)2 == (unsigned int)0 || num % (unsigned int)3 == (unsigned int)0)
          return 0;
      else if (num % (unsigned int)5 == (unsigned int)0 || num % (unsigned int)7 == (unsigned int)0)
          return 0;
      else {
          for (i = (unsigned int)11; i &lt; num; i += (unsigned int)2) {
              if (num % i == (unsigned int)0) {
                  if (i != num)
                      return 0;
                  else
                      return 1;
              }
          }
      }
      return 1;
  }
  </code></pre>
  
  <p>Test this in a file with the below code:</p>
  
  <pre><code>int main(void) {
      printf("%d\n", isprime(294967291));
      printf("%d\n", isprime(294367293));
      printf("%d\n", isprime(294967293));
      printf("%d\n", isprime(294967241)); // slow
      printf("%d\n", isprime(294967251));
      printf("%d\n", isprime(294965291));
      printf("%d\n", isprime(294966291));
      printf("%d\n", isprime(294963293));
      printf("%d\n", isprime(294927293));
      printf("%d\n", isprime(294961293));
      printf("%d\n", isprime(294917293));
      printf("%d\n", isprime(294167293));
      printf("%d\n", isprime(294267293));
      printf("%d\n", isprime(294367293)); // slow
      printf("%d\n", isprime(294467293));
      return 0;
  }
  </code></pre>
  
  <p>Results (<code>time ./test</code>):</p>
  
  <pre><code>Signed - real 0m0.949s
  Unsigned - real 0m1.174s
  </code></pre>