sd15: cython
system:sage

<h1 style="text-align: center;">Cython</h1>
<h2 style="text-align: center;">Robert Bradshaw and Craig Citro<br /></h2>
<p style="text-align: center;">&nbsp;</p>

{{{id=0|
%python
def mysum(n):
    s = 0
    for k in range(n):
        s += k
    return s
///
}}}

{{{id=4|
time mysum(10**6)
///
499999500000L
Time: CPU 0.26 s, Wall: 0.27 s
}}}

{{{id=9|
%cython
def mysum_c(n):
    cdef int k
    cdef long long s
    
    s = 0
    for k in range(n):
        s += k
    return s
///
}}}

{{{id=8|
time mysum_c(10**6)
///
499999500000L
Time: CPU 0.00 s, Wall: 0.00 s
}}}

{{{id=11|
timeit('mysum(10**6)')
///
5 loops, best of 3: 255 ms per loop
}}}

{{{id=10|
timeit('mysum_c(10**6)')
///
625 loops, best of 3: 1.23 ms per loop
}}}

{{{id=14|
255/1.23
///
207.317073170732
}}}

{{{id=13|
mysum_c(10**10)
///
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/craigcitro/.sage/sage_notebook/worksheets/admin/2/code/30.py", line 7, in <module>
    exec compile(ur'mysum_c(_sage_const_10 **_sage_const_10 )' + '\n', '', 'single')
  File "/sage/local/lib/python2.5/site-packages/SQLAlchemy-0.4.6-py2.5.egg/", line 1, in <module>
    
  File "/Users/craigcitro/.sage//temp/sharma.local/49039//spyx//_Users_craigcitro__sage_sage_notebook_worksheets_admin_2_code_sage25_spyx/_Users_craigcitro__sage_sage_notebook_worksheets_admin_2_code_sage25_spyx_0.pyx", line 11, in _Users_craigcitro__sage_sage_notebook_worksheets_admin_2_code_sage25_spyx_0.mysum_c (_Users_craigcitro__sage_sage_notebook_worksheets_admin_2_code_sage25_spyx_0.c:248)
    for k in range(n):
OverflowError: long int too large to convert to int
}}}

{{{id=12|

///
}}}

{{{id=1|
def mysum2(n):
    return sum(range(n))
///
}}}

{{{id=2|
time mysum2(10**6)
///
499999500000L
Time: CPU 0.19 s, Wall: 0.19 s
}}}

{{{id=26|

///
}}}

{{{id=25|

///
}}}

{{{id=24|
%cython

cdef class Box:
    
    cdef int _length, _width, _height
    
    def __init__(self, l, w, h):
        self._length = l
        self._width = w
        self._height = h

    def __repr__(self):
        return "Box of dimensions %s x %s x %s"%(self._length, self._width, self._height)

    def volume(self):
        return self._length * self._width * self._height

    def extend_py(self, d):
        self._length += d

    cdef extend_c(self, int d):
        self._length += d

    cpdef extend(self, int d):
        self._length += d

    def time_test(self, d, n, func='cpdef'):

        cdef int i
        cdef int delta = d

        if func == 'cpdef':
            for i in range(n):
                self.extend(delta)
        elif func == 'cdef':
            for i in range(n):
                self.extend_c(delta)
        elif func == 'def':
            for i in range(n):
                self.extend_py(d)
///
}}}

{{{id=23|
b = Box(3,4,5)
///
}}}

{{{id=22|
time b.time_test(1, 10**7, 'def')
///
Time: CPU 2.07 s, Wall: 2.19 s
}}}

{{{id=21|
time b.time_test(1, 10**7, 'cdef')
///
Time: CPU 0.06 s, Wall: 0.06 s
}}}

{{{id=20|
time b.time_test(1, 10**7, 'cpdef')
///
Time: CPU 0.09 s, Wall: 0.09 s
}}}

{{{id=19|
# look at annotated files and C source
///
}}}

{{{id=18|
233 / (233.0 + 942)
///
0.198297872340426
}}}

{{{id=17|
timeit('3+7')
///
625 loops, best of 3: 702 ns per loop
}}}

{{{id=16|
n = int(3)
m = int(7)
timeit('n+m')
///
625 loops, best of 3: 103 ns per loop
}}}

{{{id=5|
215 / (215 + 468.0)
///
0.314787701317716
}}}

{{{id=28|

///
}}}