Numpy rounding performance
<p>I am seeing some very surprising behaviour from numpy (version 1.10.4). See the following code with output:</p>

<pre><code>import numpy as np

a = np.random.rand()
b = np.random.rand(1)[0]
print('a: ',a)
print('b:', b)
print('type(a): ', type(a))
print('type(a): ', type(b))
print('round(a):', round(a))
print('round(b):', round(b))
print('\n')

%timeit round(a)
%timeit round(b)
%timeit int(round(b))



a:  0.4991662851604657
b: 0.301059130742
type(a):  &lt;class 'float'&gt;
type(a):  &lt;class 'numpy.float64'&gt;
round(a): 0
round(b): 0.0


The slowest run took 7.49 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 232 ns per loop
The slowest run took 13.84 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.19 µs per loop
The slowest run took 6.54 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.37 µs per loop
</code></pre>

<p><code>np.random.rand</code> seems to return different numerical types in two very similar cases. </p>

<p>This bit me, since I started to rely on the behaviour of Python's <code>round</code> which returns an <code>int</code> by default. If, however, the input to <code>round</code> is a <code>numpy.float64</code> it returns a <code>float</code>, I guess because <code>numpy.round</code> does. Relying on the behaviour of <code>round</code> suddenly becomes quite dangerous.</p>

<p>The performance of <code>round</code> on <code>float</code> vs <code>numpy.float64</code> is also dramatically different, so much so that it had an appreciable impact on my library.</p>

<p>Is the performance issue a bug, or is numpy overhead really <em>that</em> dramatic, even for scalar <code>floats</code>, or are there issues with the way benchmarking works here? How can I get fast rounding to <code>int</code> performance from <code>numpy.float64</code>s?</p>

<p>Finally, why does <code>rand()</code> return a different type than <code>rand(1)[0]</code>?</p>