title: Is array.count() orders of magnitude slower than list.count() in Python?
body: <p>Currently I am playing with Python performance, trying to speed up my programs (usually those which compute heuristics). I always used lists, trying not to get into <code>numpy</code> arrays.</p>
  
  <p>But recently I've heard that Python has <a href="https://docs.python.org/3/library/array.html" rel="nofollow noreferrer"><strong><code>8.7. array — Efficient arrays of numeric values</code></strong></a> so I thought I would try that one.</p>
  
  <p>I wrote a piece of code to measure an <code>array.count()</code> vs. a <code>list.count()</code>, as I use it in many places in my code:</p>
  
  <pre><code>from timeit import timeit
  import array
  
  a = array.array('i', range(10000))
  l = [range(10000)]
  
  
  def lst():
      return l.count(0)
  
  
  def arr():
      return a.count(0)
  
  
  print(timeit('lst()', "from __main__ import lst", number=100000))
  print(timeit('arr()', "from __main__ import arr", number=100000))
  </code></pre>
  
  <p>I was expecting a slight performance improvement when using <code>array</code>. Well, this is what happened:</p>
  
  <pre><code>&gt; python main.py
  0.03699162653848456
  74.46420751473268
  </code></pre>
  
  <p>So, according to <code>timeit</code> the <code>list.count()</code> is 2013x faster than the <code>array.count()</code>. I definitely didn't expect that. So I've searched through SO, python docs etc. and the only thing I found was that the objects in array have to be first wrapped into <code>int</code>-s, so this could slow things down, but I was expecting this to happen when creating an <code>array.array</code>-instance, not when random accessing it (which I believe is what <code>.count()</code> does).</p>
  
  <p>So where's the catch?</p>
  
  <p>Am I doing something wrong?</p>
  
  <p>Or maybe I shouldn't use standard arrays and go straight to <code>numpy.array</code>s?</p>