Notebook

# Namedtuple vs Extension Class¶

In [1]:

    %load_ext Cython

In [5]:

    %%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 [6]:

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

In [7]:

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

In [8]:

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

In [9]:

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

    10000 loops, best of 3: 65.5 µs per loop
    1000 loops, best of 3: 211 µs per loop

In []: