title: Why are some float < integer comparisons four times slower than others?
body: <p>When comparing floats to integers, some pairs of values take much longer to be evaluated than other values of a similar magnitude.</p>
  
  <p>For example:</p>
  
  <pre><code>&gt;&gt;&gt; import timeit
  &gt;&gt;&gt; timeit.timeit("562949953420000.7 &lt; 562949953421000") # run 1 million times
  0.5387085462592742
  </code></pre>
  
  <p>But if the float or integer is made smaller or larger by a certain amount, the comparison runs much more quickly:</p>
  
  <pre><code>&gt;&gt;&gt; timeit.timeit("562949953420000.7 &lt; 562949953422000") # integer increased by 1000
  0.1481498428446173
  &gt;&gt;&gt; timeit.timeit("562949953423001.8 &lt; 562949953421000") # float increased by 3001.1
  0.1459577925548956
  </code></pre>
  
  <p>Changing the comparison operator (e.g. using <code>==</code> or <code>&gt;</code> instead) does not affect the times in any noticeable way. </p>
  
  <p>This is not <em>solely</em> related to magnitude because picking larger or smaller values can result in faster comparisons, so I suspect it is down to some unfortunate way the bits line up. </p>
  
  <p>Clearly, comparing these values is more than fast enough for most use cases. I am simply curious as to why Python seems to struggle more with some pairs of values than with others.</p>