Self-built extension module slower than built-in c module
<p>To learn how to create C-extensions I've decided to just copy a built-in <code>.c</code>-file (in this case <a href="https://github.com/python/cpython/blob/master/Modules/itertoolsmodule.c" rel="noreferrer"><code>itertoolsmodule.c</code></a>) and placed it in my package. I only changed the names inside the module from <code>itertools</code> to <code>mypkg</code>.</p>

<p>Then I compiled it (Windows 10, MSVC Community 14) as <code>setuptools.Extension</code>:</p>

<pre><code>from setuptools import setup, Extension

itertools_module = Extension('mypkg.itertoolscopy',
                              sources=['src/itertoolsmodulecopy.c'])

setup(...
      ext_modules=[itertools_module])
</code></pre>

<p>The default uses the compiler flags <code>/c /nologo /Ox /W3 /GL /DNDEBUG /MD</code> and I read somewhere that these defaults equals the settings of how the python was compiled. However I use conda (64bit setup) so this might <em>not necessarily</em> be true.</p>

<p>It all went well - but a benchmark for <code>filterfalse</code> showed that it's almost a factor 2 slower than the built-in:</p>

<pre><code>import mypkg
import itertools

import random

a = [random.random() for _ in range(500000)]
func = None

%timeit list(filter(func, a))
100 loops, best of 3: 3.42 ms per loop
%timeit list(itertools.filterfalse(func, a))
100 loops, best of 3: 3.41 ms per loop
%timeit list(mypkg.filterfalse(func, a))
100 loops, best of 3: 6.77 ms per loop
</code></pre>

<p>However, for smaller iterables the discrepancy also becomes smaller:</p>

<pre><code>a = [random.random() for _ in range(500)]  # 1 / 1000 of the elements

%timeit list(filter(func, a))
100000 loops, best of 3: 9.66 µs per loop
%timeit list(itertools.filterfalse(func, a))
100000 loops, best of 3: 10.8 µs per loop
%timeit list(mypkg.filterfalse(func, a))
100000 loops, best of 3: 14.4 µs per loop
</code></pre>

<p>I wasn't able to explain this difference in speed but I have to admit that I'm not too familiar with compiling <code>C</code>-code. I'm at a loss what actually makes it slower. </p>

<p>The results are the same on python 2.7 with <code>ifilter</code> and <code>ifilterfalse</code> and the 2.7 version of the <code>itertoolsmodule.c</code> file.</p>

<p>Does anyone knows what makes the code perform worse than the built-ins and how one could speed it up?</p>