Why is hash() slower under python3.4 vs python2.7
<p>I was doing some performance evaluation using timeit and discovered a performance degredation between python 2.7.10 and python 3.4.3. I narrowed it down to the <code>hash()</code> function:</p>

<p>python 2.7.10:</p>

<pre><code>&gt;&gt;&gt; import timeit
&gt;&gt;&gt; timeit.timeit('for x in xrange(100): hash(x)', number=100000)
0.4529099464416504
&gt;&gt;&gt; timeit.timeit('hash(1000)')
0.044638872146606445
</code></pre>

<p>python 3.4.3:</p>

<pre><code>&gt;&gt;&gt; import timeit
&gt;&gt;&gt; timeit.timeit('for x in range(100): hash(x)', number=100000)
0.6459149940637872
&gt;&gt;&gt; timeit.timeit('hash(1000)')
0.07708719989750534
</code></pre>

<p>That's an approx. 40% degradation! It doesn't seem to matter if integers, floats, strings(unicodes or bytearrays), etc, are being hashed; the degradation is about the same. In both cases the hash is returning a 64-bit integer. The above was run on my Mac, and got a smaller degradation (20%) on an Ubuntu box.</p>

<p>I've also used PYTHONHASHSEED=random for the python2.7 tests and in <em>some</em> cases, restarting python for each "case", I saw the <code>hash()</code> performance get a bit worse, but never as slow as python3.4</p>

<p>Anyone know what's going on here? Was a more-secure, but slower, hash function chosen for python3 ?</p>

<p>There are two changes in <code>hash()</code> function between Python 2.7 and Python 3.4</p>

<ol>
<li>Adoptions of <em>SipHash</em></li>
<li>Default enabling of <em>Hash randomization</em></li>
</ol>

<hr>

<p><em>References:</em></p>

<ul>
<li>Since from Python 3.4, it uses <a href="https://131002.net/siphash/" rel="nofollow">SipHash</a> for it's hashing function. Read: <a href="https://lwn.net/Articles/574761/" rel="nofollow">Python adopts SipHash</a></li>
<li>Since Python 3.3 <em>Hash randomization is enabled by default.</em> Reference: <a href="https://docs.python.org/3/reference/datamodel.html#object.__hash__" rel="nofollow"><code>object.__hash__</code></a> (last line of this section). Specifying <a href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED" rel="nofollow"><code>PYTHONHASHSEED</code></a> the value 0 will disable hash randomization.</li>
</ul>

python python-3.4