Namedtuple

# Namedtuple vs Extension Class¶

In [1]:

    %load_ext Cython

In [2]:

    %%cython

    cdef class InvalRequestExt:
        cdef int ino
        cdef char attr_only

        def __cinit__(self, ino, attr_only):
            self.ino = ino
            self.attr_only = bool(attr_only)

In [3]:

    from collections import namedtuple
    InvalRequestTup = namedtuple('InvalRequestTup', [ 'inode', 'attr_only' ])

In [4]:

    def test(cls):
        inst = []
        for i in range(500):
            inst.append(cls(i, False))
        return inst

In [5]:

    assert len(test(InvalRequestExt)) == len(test(InvalRequestTup))

In [6]:

    %timeit test(InvalRequestExt)
    %timeit test(InvalRequestTup)

    10000 loops, best of 3: 63.3 µs per loop
    1000 loops, best of 3: 204 µs per loop

In [ ]: