summaryrefslogtreecommitdiff
path: root/Doc/s5/cython-ep2008.txt
blob: a29ca35d65c567b26cf52ad3c01e4a8a3a776d64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
==============================================
The Cython Compiler for C-Extensions in Python
==============================================

Dr. Stefan Behnel
-----------------

.. class:: center

  http://www.cython.org/

  cython-dev@codespeak.net

.. footer:: Dr. Stefan Behnel, EuroPython 2008, Vilnius/Lietuva

.. include:: <s5defs.txt>


About myself
============

* Passionate Python developer since 2002

  * after Basic, Logo, Pascal, Prolog, Scheme, Java, ...

* CS studies in Germany, Ireland, France

* PhD in distributed systems in 2007

  * Language design for self-organising systems

  * Darmstadt University of Technologies, Germany

* Current occupations:

  * Employed by Senacor Technologies AG, Germany

    * IT transformations, SOA design, Java-Development, ...

  * »lxml« OpenSource XML toolkit for Python

    * http://codespeak.net/lxml/

  * Cython


What is Cython?
===============

Cython is

* an Open-Source project

  * http://cython.org

* a Python compiler (almost)

  * an enhanced, optimising fork of Pyrex

* an extended Python language for

  * writing fast Python extension modules

  * interfacing Python with C libraries


A little bit of history
=======================

* April 2002: release of Pyrex 0.1 by Greg Ewing

  * Greg considers Pyrex a language in design phase

  * Pyrex became a key language for many projects

    * over the years, many people patched their Pyrex

    * not all patches were answered by Greg (not in time)

* minor forks and enhanced branches followed

  * March 2006: my fork of Pyrex for »lxml« XML toolkit

  * November 2006: »SageX« fork of Pyrex

    * by Robert Bradshaw, William Stein (Univ. Seattle, USA)

    * context: »Sage«, a free mathematics software package

* 28th July 2007: official Cython launch

  * integration of lxml's Pyrex fork into SageX

  * the rest is in http://hg.cython.org/cython-devel/


Major Cython Developers
=======================

* Robert Bradshaw and Stefan Behnel

  * lead developers

* Greg Ewing

  * main developer and maintainer of Pyrex

  * we happily share code and discuss ideas

* Dag Sverre Seljebotn

  * Google Summer-of-Code developer

  * NumPy integration, many ideas and enhancements

* many, *many* others - see

  * http://cython.org/

  * the mailing list archives of Cython and Pyrex


How to use Cython
=================

* you write Python code

  * Cython translates it into C code

  * your C compiler builds a shared library for CPython

  * you import your module

* Cython has support for

  * compile-time includes/imports

    * with dependency tracking

  * distutils

    * *optionally* compile Python code from setup.py!


Example: compiling Python code
==============================

.. sourcecode:: bash

    $ cat worker.py

.. sourcecode:: python

    class HardWorker(object):
        u"Almost Sisyphus"
        def __init__(self, task):
            self.task = task

        def work_hard(self):
            for i in range(100):
	        self.task()

.. sourcecode:: bash

    $ cython worker.py

* translates to 842 line `.c file <ep2008/worker.c>`_ (Cython 0.9.8)

  * lots of portability #define's

  * tons of helpful C comments with Python code snippets

  * a lot of code that you don't want to write yourself


Portable Code
=============

* Cython compiler generates C code that compiles

  * with all major compilers (C and C++)

  * on all major platforms

  * in Python 2.3 to 3.0 beta1

* Cython language syntax follows Python 2.6

  * optional Python 3 syntax support is on TODO list

    * get involved to get it quicker!

\... the fastest way to port Python 2 code to Py3 ;-)


Python 2 feature support
========================

* most of Python 2 syntax is supported

  * top-level classes and functions

  * exceptions

  * object operations, arithmetic, ...

* plus some Py3/2.6 features:

  * keyword-only arguments

  * unicode literals via ``__future__`` import

* in recent developer branch:

  * ``with`` statement

  * closures (i.e. local classes and functions) are close!


Speed
=====

Cython generates very efficient C code

* according to PyBench:

  * conditions and loops run 2-8x faster than in Py2.5

  * most benchmarks run 30%-80% faster

  * overall more than 30% faster for plain Python code

* optional type declarations

  * let Cython generate plain C instead of C-API calls

  * make code several times faster than the above


Type declarations
=================

* »cdef« keyword declares

  * local variables with C types

  * functions with C signatures

  * classes as builtin extension types

* Example::

    def stupid_lower_case(char* s):
        cdef Py_ssize_t size, i

	size = len(s)
        for i in range(size):
            if s[i] >= 'A' and s[i] <= 'Z':
                s[i] += 'a' - 'A'
        return s


Why is this stupid?
===================

Ask Cython!

.. sourcecode:: bash

    $ cat stupidlowercase.py

::

    def stupid_lower_case(char* s):
        cdef Py_ssize_t size, i

	size = len(s)
        for i in range(size):
            if s[i] >= 'A' and s[i] <= 'Z':
                s[i] += 'a' - 'A'
        return s

.. sourcecode:: bash

    $ cython --annotate stupidlowercase.py

=> `stupidlowercase.html <ep2008/stupidlowercase.html>`_


Calling C functions
===================

* Example::

    cdef extern from "Python.h":
        # copied from the Python C-API docs:
        object PyUnicode_DecodeASCII(
            char* s, Py_ssize_t size, char* errors)

    cdef extern from "string.h":
        int strlen(char *s)


    cdef slightly_better_lower_case(char* s):
        cdef Py_ssize_t i, size = strlen(s)
        for i in range(size):
            if s[i] >= 'A' and s[i] <= 'Z':
                s[i] += 'a' - 'A'
	return PyUnicode_DecodeASCII(s, size, NULL)


Features in work
================

* Dynamic classes and functions with closures

  .. sourcecode:: python

    def factory(a,b):
        def closure_function(c):
            return a+b+c
        return closure_function

* Native support for new ``buffer`` protocol

  * part of NumPy integration by Dag Seljebotn

  .. sourcecode:: python

    def inplace_negative_grayscale_image(
                      ndarray[unsigned char, 2] image):
        cdef int i, j
        for i in range(image.shape[0]):
            for j in range(image.shape[1]):
                arr[i, j] = 255 - arr[i, j]


Huge pile of great ideas
========================

* Cython Enhancement Proposals (CEPs)

  * http://wiki.cython.org/enhancements

* native pickle support for extension classes

* meta-programming facilities

* type inference strategies

* compile-time assertions for optimisations

* object-like C-array handling

* improved C++ integration

* ...


Conclusion
==========

* Cython is a tool for

  * efficiently translating Python code to C

  * easily interfacing to external C libraries

* Use it to

  * speed up existing Python modules

    * concentrate on optimisations, not rewrites!

  * write C extensions for CPython

    * don't change the language just to get fast code!

  * wrap C libraries *in Python*

    * concentrate on the mapping, not the glue!


... but Cython is also
======================

* a great project

* a very open playground for great ideas!


Cython
======

  **Cython**

  **C-Extensions in Python**

  \... use it, and join the project!

  http://cython.org/