title: Why random.randint() is much slower than random.getrandbits()
body: <p>A made a test which compares <code>random.randint()</code> and <code>random.getrandbits()</code> in Python. The result shows that <code>getrandbits</code> is way faster than <code>randint</code>.</p>
  <pre class="lang-py prettyprint-override"><code>from random import randint, getrandbits, seed
  from datetime import datetime
  
  def ts():
      return datetime.now().timestamp()
  
  def diff(st, en):
      print(f'{round((en - st) * 1000, 3)} ms')
  
  seed(111)
  N = 6
  
  rn = []
  st = ts()
  for _ in range(10**N):
      rn.append(randint(0, 1023))
  en = ts()
  diff(st,en)
  
  rn = []
  st = ts()
  for _ in range(10**N):
      rn.append(getrandbits(10))
  en = ts()
  diff(st,en)
  </code></pre>
  <p>The numbers range is exactly the same, because <code>10</code> random bits is range from <code>0</code> (in case of <code>0000000000</code>) to <code>1023</code> (in case of <code>1111111111</code>).</p>
  <p>The output of the code:</p>
  <pre><code>590.509 ms
  91.01 ms
  </code></pre>
  <p>As you can see, <code>getrandbits</code> under the same conditions is almost <code>6.5</code> times faster. But why? Can somebody explain that?</p>