title: Simpe Loop squaring Integer Values is slower in C than in PHP
body: <p>Yesterday I watched a nice (short) video about a mathematics problem with the number '33'.
  Link: <a href="https://www.youtube.com/watch?v=wymmCdLdPvM" rel="nofollow">https://www.youtube.com/watch?v=wymmCdLdPvM</a></p>
  
  <p>In short: Many numbers can be calculated by: <code>a³ + b³ + c³</code></p>
  
  <p>For example: <code>29 = 3³ + 1³ + 1³</code>  </p>
  
  <p>(should be mentioned: <code>a</code>, <code>b</code>, <code>c</code> can be negative!)</p>
  
  <p>So, I though I give the problem a short bruteforce try. Nothing sophisticated, just to see how fast I can compute those values.</p>
  
  <p>I built 3 Programs (PHP, Pyhton and C, let's concentrate on PHP and C here), and thats what I've got...</p>
  
  <p>PHP-Program:</p>
  
  <pre><code>#!/usr/bin/php5
  &lt;?php
  
  $total = 0;
  $aktstamp = time();
  
  for ($a = -100000000000; $a &lt;= 100000000000; $a++) {
      for ($b = -100000000000; $b &lt;= 100000000000; $b++) {
          for ($c = -100000000000; $c &lt;= 100000000000; $c++) {
              $total++;
  
              if ($a**3+$b**3+$c**3 == 33) {
                  echo "FOUND IT! a**3+$b**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
              }
              if ((-$a)**3+$b**3+$c**3 == 33) {
                  echo "FOUND IT! (-a)**3+$b**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
              }                   
              if ($a**3+(-$b)**3+$c**3 == 33) {
                  echo "FOUND IT! a**3+(-$b)**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
              }                   
              if ($a**3+$b**3+(-$c)**3 == 33) {
                  echo "FOUND IT! a**3+$b**3+(-$c)**3 a: " . $a . " b: " . $b + " c: " . $c;
              }                   
              if ((-$a)**3+(-$b)**3+$c**3 == 33) {
                  echo "FOUND IT! (-a)**3+(-$b)**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
              }                   
              if ((-$a)**3+$b**3+(-$c)**3 == 33) {
                  echo "FOUND IT! (-a)**3+$b**3+(-$c)**3 a: " . $a . " b: " . $b + " c: " . $c;
              }                   
              if ($a**3+(-$b)**3+(-$c)**3 == 33) {
                  echo "FOUND IT! a**3+(-$b)**3+(-$c)**3 a: " . $a . " b: " . $b + " c: " . $c;  
              }
  
              if ($total % 10000000 == 0) {
                  $timetaken = time() - $aktstamp;
                  $calcspersec = 10000000 / $timetaken;
  
                  $date = date("d.m.Y", time());
                  $time = date("H:i:s", time());
                  echo $date . " " . $time . ": " . $a . "\n";
                  echo "(Calcs per sec: " . $calcspersec . ")\n";
  
                  $aktstamp = time();         
              }
          }
      }
  }
  ?&gt;
  </code></pre>
  
  <p>And a C Program:</p>
  
  <pre><code>/* 
  Compile with: 
  gcc ./33.c -o 33 -lm -O1
  */
  
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  #include &lt;sys/time.h&gt;
  
  long long main(long long argc, char *argv[])
  {
      long long total = 0, a = 0, b = 0, c = 0;
      int timetaken, calcspersec;
      int aktstamp = (int)time(NULL);
  
      for (a = -100000000000; a &lt;= 100000000000; a++) {
          for (b = -100000000000; b &lt;= 100000000000; b++) {
              for (c = -100000000000; c &lt;= 100000000000; c++) {
                  total++;
  
                  if (pow(a, 3)+pow(b, 3)+pow(c, 3) == 33) {
                      printf("FOUND IT! pow(a, 3)+pow(b, 3)+pow(c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
                  }
                  if (pow(-a, 3)+pow(b, 3)+pow(c, 3) == 33) {
                      printf("FOUND IT! pow(-a, 3)+pow(b, 3)+pow(c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
                  }                   
                  if (pow(a, 3)+pow(-b, 3)+pow(c, 3) == 33) {
                      printf("FOUND IT! pow(a, 3)+pow(-b, 3)+pow(c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
                  }                   
                  if (pow(a, 3)+pow(b, 3)+pow(-c, 3) == 33) {
                      printf("FOUND IT! pow(a, 3)+pow(b, 3)+pow(-c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
                  }                   
                  if (pow(-a, 3)+pow(-b, 3)+pow(c, 3) == 33) {
                      printf("FOUND IT! pow(-a, 3)+pow(-b, 3)+pow(c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
                  }                   
                  if (pow(-a, 3)+pow(b, 3)+pow(-c, 3) == 33) {
                      printf("FOUND IT! pow(-a, 3)+pow(b, 3)+pow(-c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
                  }                   
                  if (pow(a, 3)+pow(-b, 3)+pow(-c, 3) == 33) {
                      printf("FOUND IT! pow(a, 3)+pow(-b, 3)+pow(-c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
                  }
  
                  if (total % 10000000 == 0) {
                      timetaken = (int)time(NULL) - aktstamp;
                      calcspersec = 10000000 / timetaken;
  
                      printf("%lld\n", a);
                      printf("(Calcs per sec: %u)\n", calcspersec);
  
                      aktstamp = time(NULL);          
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Here are the Speeds:</p>
  
  <pre><code>PHP: (Calcs per sec: 2000000)
  Python: (Calcs per sec: 1186440)
  C: (Calcs per sec: 833333)
  </code></pre>
  
  <p>It was no suprise to me that PHP was faster than Python. But why on earth is C so much slower than PHP?</p>
  
  <p>Even Python is faster than C...</p>
  
  <p>Do I have a stupid error, or is the C-pow() Function so much slower than PHPs <code>**</code> ?</p>
  
  <p>So, does anyone see a obvious reason for why C is half as fast as PHP?</p>