Roman Korolev
Roman Korolev

Reputation: 21

Why is itertools izip faster than equivalent function from documentation?

The documentation is written:

def izip(*iterables):
    # izip('ABCD', 'xy') --> Ax By
    iterators = map(iter, iterables)
    while iterators:
        yield tuple(map(next, iterators))

But when i run test with itertools.izip it works faster, then when I run izip function from my module. Why is this happening? Can I run the module function as fast?

Timer test:

>>> t = Timer("dict(izip_2(keys,values))", "from __main__ import keys, values, izip_2")
>>> t2 = Timer("dict(itertools.izip(keys,values))", "from __main__ import keys, values")
>>> 
>>> print min(t.repeat(5, 10000))
31.6810410023
>>> print min(t2.repeat(5, 10000))
2.50448894501

Upvotes: 1

Views: 193

Answers (1)

NPE
NPE

Reputation: 500673

In my CPython, itertools is coded in C rather than Python:

In [1]: import itertools

In [2]: itertools.__file__
Out[2]: '.../lib/python2.7/lib-dynload/itertools.so'

Generally, precompiled C tends to be faster than interpreted Python, so this could well be one reason.

Upvotes: 2

Related Questions