title: numpy float: 10x slower than builtin in arithmetic operations?
body: <p>I am getting really weird timings for the following code:</p>
  
  <pre><code>import numpy as np
  s = 0
  for i in range(10000000):
      s += np.float64(1) # replace with np.float32 and built-in float
  </code></pre>
  
  <ul>
  <li>built-in float: 4.9 s</li>
  <li>float64: 10.5 s</li>
  <li>float32: 45.0 s</li>
  </ul>
  
  <p>Why is <code>float64</code> twice slower than <code>float</code>? And why is <code>float32</code> 5 times slower than float64?</p>
  
  <p>Is there any way to avoid the penalty of using <code>np.float64</code>, and have <code>numpy</code> functions return built-in <code>float</code> instead of <code>float64</code>?</p>
  
  <p>I found that using <code>numpy.float64</code> is much slower than Python's float, and <code>numpy.float32</code> is even slower (even though I'm on a 32-bit machine).</p>
  
  <p><code>numpy.float32</code> on my 32-bit machine. Therefore, every time I use various numpy functions such as <code>numpy.random.uniform</code>, I convert the result to <code>float32</code> (so that further operations would be performed at 32-bit precision).</p>
  
  <p>Is there any way to set a single variable somewhere in the program or in the command line, and make all numpy functions return <code>float32</code> instead of <code>float64</code>?</p>
  
  <p><strong>EDIT #1:</strong></p>
  
  <p>numpy.float64 is <strong><em>10 times</em></strong> slower than float in arithmetic calculations. It's so bad that even converting to float and back before the calculations makes the program run 3 times faster. Why? Is there anything I can do to fix it?</p>
  
  <p>I want to emphasize that my timings are not due to any of the following:</p>
  
  <ul>
  <li>the function calls</li>
  <li>the conversion between numpy and python float</li>
  <li>the creation of objects</li>
  </ul>
  
  <p>I updated my code to make it clearer where the problem lies. With the new code, it would seem I see a ten-fold performance hit from using numpy data types:</p>
  
  <pre><code>from datetime import datetime
  import numpy as np
  
  START_TIME = datetime.now()
  
  # one of the following lines is uncommented before execution
  #s = np.float64(1)
  #s = np.float32(1)
  #s = 1.0
  
  for i in range(10000000):
      s = (s + 8) * s % 2399232
  
  print(s)
  print('Runtime:', datetime.now() - START_TIME)
  </code></pre>
  
  <p>The timings are:</p>
  
  <ul>
  <li>float64: 34.56s</li>
  <li>float32: 35.11s</li>
  <li>float: 3.53s</li>
  </ul>
  
  <p>Just for the hell of it, I also tried:</p>
  
  <p>from datetime import datetime
  import numpy as np</p>
  
  <pre><code>START_TIME = datetime.now()
  
  s = np.float64(1)
  for i in range(10000000):
      s = float(s)
      s = (s + 8) * s % 2399232
      s = np.float64(s)
  
  print(s)
  print('Runtime:', datetime.now() - START_TIME)
  </code></pre>
  
  <p>The execution time is 13.28 s; it's actually 3 times faster to convert the <code>float64</code> to <code>float</code> and back than to use it as is. Still, the conversion takes its toll, so overall it's more than 3 times slower compared to the pure-python <code>float</code>.</p>
  
  <p>My machine is:</p>
  
  <ul>
  <li>Intel Core 2 Duo T9300 (2.5GHz)</li>
  <li>WinXP Professional (32-bit)</li>
  <li>ActiveState Python 3.1.3.5</li>
  <li>Numpy 1.5.1</li>
  </ul>
  
  <p><strong>EDIT #2:</strong></p>
  
  <p>Thank you for the answers, they help me understand how to deal with this problem.</p>
  
  <p>But I still would like to know the precise reason (based on the source code perhaps) why the code below runs 10 times slow with <code>float64</code> than with <code>float</code>.</p>
  
  <p><strong>EDIT #3:</strong></p>
  
  <p>I rerun the code under the Windows 7 x64 (Intel Core i7 930 @ 3.8GHz).</p>
  
  <p>Again, the code is:</p>
  
  <pre><code>from datetime import datetime
  import numpy as np
  
  START_TIME = datetime.now()
  
  # one of the following lines is uncommented before execution
  #s = np.float64(1)
  #s = np.float32(1)
  #s = 1.0
  
  for i in range(10000000):
      s = (s + 8) * s % 2399232
  
  print(s)
  print('Runtime:', datetime.now() - START_TIME)
  </code></pre>
  
  <p>The timings are:</p>
  
  <ul>
  <li>float64: 16.1s</li>
  <li>float32: 16.1s</li>
  <li>float: 3.2s</li>
  </ul>
  
  <p>Now both <code>np</code> floats (either 64 or 32) are 5 times slower than the built-in <code>float</code>. Still, a significant difference. I'm trying to figure out where it comes from.</p>
  
  <p><strong>END OF EDITS</strong></p>