{
 "metadata": {
  "name": "",
  "signature": "sha256:02c3b7cea09d17093dba2c8131d710f060cd531a184028da1d4c350626243981"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Namedtuple vs Extension Class"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%load_ext Cython"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%cython\n",
      "\n",
      "cdef class InvalRequestExt:\n",
      "    cdef int ino\n",
      "    cdef char attr_only\n",
      "    \n",
      "    def __cinit__(self, ino, attr_only):\n",
      "        self.ino = ino\n",
      "        self.attr_only = bool(attr_only)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import namedtuple\n",
      "InvalRequestTup = namedtuple('InvalRequestTup', [ 'inode', 'attr_only' ])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def test(cls):\n",
      "    inst = []\n",
      "    for i in range(500):\n",
      "        inst.append(cls(i, False))\n",
      "    return inst"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "assert len(test(InvalRequestExt)) == len(test(InvalRequestTup))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%timeit test(InvalRequestExt)\n",
      "%timeit test(InvalRequestTup)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10000 loops, best of 3: 65.5 \u00b5s per loop\n",
        "1000 loops, best of 3: 211 \u00b5s per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}