Are Python modules compiled?
<p>Trying to understand whether python libraries are compiled because I want to know if the interpreted code I write will perform the same or worse.</p>

<p>e.g. I saw it mentioned somewhere that numpy and scipy are efficient because they are compiled. I don't think this means byte code compiled so how was this done? Was it compiled to c using something like cython? Or was it written using a language like c and compiled in a compatible way?</p>

<p>Does this apply to all modules or is it on a case-by-case basis?</p>

<p>NumPy and several other libraries are partly wrappers for code written in C and other languages like FORTRAN, which when compiled will run faster than Python. This helps by avoiding the cost of loops, pointer indirection and per-element dynamic type checking in Python. This is explained in <a href="http://stackoverflow.com/questions/8385602/why-are-numpy-arrays-so-fast">this question</a>:</p>

<blockquote>
  <p>Numpy arrays are densely packed arrays of homogeneous type. Python lists, by contrast, are arrays of pointers to objects, even when all of them are of the same type. So, you get the benefits of locality of reference.</p>
  
  <p>Also, many Numpy operations are implemented in C, avoiding the general cost of loops in Python, pointer indirection and per-element dynamic type checking. The speed boost depends on which operations you're performing, but a few orders of magnitude isn't uncommon in number crunching programs.</p>
</blockquote>

<p>Python code that is compiled to bytecode (.pyc files) is a separate topic, in which python scripts are compiled to increase startup performance (see <a href="http://stackoverflow.com/questions/471191/why-compile-python-code">this question</a>).</p>

python numpy compilation