a29ca35d65c567b26cf52ad3c01e4a8a3a776d64
[cython.git] / Doc / s5 / cython-ep2008.txt
1 ==============================================
2 The Cython Compiler for C-Extensions in Python
3 ==============================================
4
5 Dr. Stefan Behnel
6 -----------------
7
8 .. class:: center
9
10   http://www.cython.org/
11
12   cython-dev@codespeak.net
13
14 .. footer:: Dr. Stefan Behnel, EuroPython 2008, Vilnius/Lietuva
15
16 .. include:: <s5defs.txt>
17
18
19 About myself
20 ============
21
22 * Passionate Python developer since 2002
23
24   * after Basic, Logo, Pascal, Prolog, Scheme, Java, ...
25
26 * CS studies in Germany, Ireland, France
27
28 * PhD in distributed systems in 2007
29
30   * Language design for self-organising systems
31
32   * Darmstadt University of Technologies, Germany
33
34 * Current occupations:
35
36   * Employed by Senacor Technologies AG, Germany
37
38     * IT transformations, SOA design, Java-Development, ...
39
40   * »lxml« OpenSource XML toolkit for Python
41
42     * http://codespeak.net/lxml/
43
44   * Cython
45
46
47 What is Cython?
48 ===============
49
50 Cython is
51
52 * an Open-Source project
53
54   * http://cython.org
55
56 * a Python compiler (almost)
57
58   * an enhanced, optimising fork of Pyrex
59
60 * an extended Python language for
61
62   * writing fast Python extension modules
63
64   * interfacing Python with C libraries
65
66
67 A little bit of history
68 =======================
69
70 * April 2002: release of Pyrex 0.1 by Greg Ewing
71
72   * Greg considers Pyrex a language in design phase
73
74   * Pyrex became a key language for many projects
75
76     * over the years, many people patched their Pyrex
77
78     * not all patches were answered by Greg (not in time)
79
80 * minor forks and enhanced branches followed
81
82   * March 2006: my fork of Pyrex for »lxml« XML toolkit
83
84   * November 2006: »SageX« fork of Pyrex
85
86     * by Robert Bradshaw, William Stein (Univ. Seattle, USA)
87
88     * context: »Sage«, a free mathematics software package
89
90 * 28th July 2007: official Cython launch
91
92   * integration of lxml's Pyrex fork into SageX
93
94   * the rest is in http://hg.cython.org/cython-devel/
95
96
97 Major Cython Developers
98 =======================
99
100 * Robert Bradshaw and Stefan Behnel
101
102   * lead developers
103
104 * Greg Ewing
105
106   * main developer and maintainer of Pyrex
107
108   * we happily share code and discuss ideas
109
110 * Dag Sverre Seljebotn
111
112   * Google Summer-of-Code developer
113
114   * NumPy integration, many ideas and enhancements
115
116 * many, *many* others - see
117
118   * http://cython.org/
119
120   * the mailing list archives of Cython and Pyrex
121
122
123 How to use Cython
124 =================
125
126 * you write Python code
127
128   * Cython translates it into C code
129
130   * your C compiler builds a shared library for CPython
131
132   * you import your module
133
134 * Cython has support for
135
136   * compile-time includes/imports
137
138     * with dependency tracking
139
140   * distutils
141
142     * *optionally* compile Python code from setup.py!
143
144
145 Example: compiling Python code
146 ==============================
147
148 .. sourcecode:: bash
149
150     $ cat worker.py
151
152 .. sourcecode:: python
153
154     class HardWorker(object):
155         u"Almost Sisyphus"
156         def __init__(self, task):
157             self.task = task
158
159         def work_hard(self):
160             for i in range(100):
161                 self.task()
162
163 .. sourcecode:: bash
164
165     $ cython worker.py
166
167 * translates to 842 line `.c file <ep2008/worker.c>`_ (Cython 0.9.8)
168
169   * lots of portability #define's
170
171   * tons of helpful C comments with Python code snippets
172
173   * a lot of code that you don't want to write yourself
174
175
176 Portable Code
177 =============
178
179 * Cython compiler generates C code that compiles
180
181   * with all major compilers (C and C++)
182
183   * on all major platforms
184
185   * in Python 2.3 to 3.0 beta1
186
187 * Cython language syntax follows Python 2.6
188
189   * optional Python 3 syntax support is on TODO list
190
191     * get involved to get it quicker!
192
193 \... the fastest way to port Python 2 code to Py3 ;-)
194
195
196 Python 2 feature support
197 ========================
198
199 * most of Python 2 syntax is supported
200
201   * top-level classes and functions
202
203   * exceptions
204
205   * object operations, arithmetic, ...
206
207 * plus some Py3/2.6 features:
208
209   * keyword-only arguments
210
211   * unicode literals via ``__future__`` import
212
213 * in recent developer branch:
214
215   * ``with`` statement
216
217   * closures (i.e. local classes and functions) are close!
218
219
220 Speed
221 =====
222
223 Cython generates very efficient C code
224
225 * according to PyBench:
226
227   * conditions and loops run 2-8x faster than in Py2.5
228
229   * most benchmarks run 30%-80% faster
230
231   * overall more than 30% faster for plain Python code
232
233 * optional type declarations
234
235   * let Cython generate plain C instead of C-API calls
236
237   * make code several times faster than the above
238
239
240 Type declarations
241 =================
242
243 * »cdef« keyword declares
244
245   * local variables with C types
246
247   * functions with C signatures
248
249   * classes as builtin extension types
250
251 * Example::
252
253     def stupid_lower_case(char* s):
254         cdef Py_ssize_t size, i
255
256         size = len(s)
257         for i in range(size):
258             if s[i] >= 'A' and s[i] <= 'Z':
259                 s[i] += 'a' - 'A'
260         return s
261
262
263 Why is this stupid?
264 ===================
265
266 Ask Cython!
267
268 .. sourcecode:: bash
269
270     $ cat stupidlowercase.py
271
272 ::
273
274     def stupid_lower_case(char* s):
275         cdef Py_ssize_t size, i
276
277         size = len(s)
278         for i in range(size):
279             if s[i] >= 'A' and s[i] <= 'Z':
280                 s[i] += 'a' - 'A'
281         return s
282
283 .. sourcecode:: bash
284
285     $ cython --annotate stupidlowercase.py
286
287 => `stupidlowercase.html <ep2008/stupidlowercase.html>`_
288
289
290 Calling C functions
291 ===================
292
293 * Example::
294
295     cdef extern from "Python.h":
296         # copied from the Python C-API docs:
297         object PyUnicode_DecodeASCII(
298             char* s, Py_ssize_t size, char* errors)
299
300     cdef extern from "string.h":
301         int strlen(char *s)
302
303
304     cdef slightly_better_lower_case(char* s):
305         cdef Py_ssize_t i, size = strlen(s)
306         for i in range(size):
307             if s[i] >= 'A' and s[i] <= 'Z':
308                 s[i] += 'a' - 'A'
309         return PyUnicode_DecodeASCII(s, size, NULL)
310
311
312 Features in work
313 ================
314
315 * Dynamic classes and functions with closures
316
317   .. sourcecode:: python
318
319     def factory(a,b):
320         def closure_function(c):
321             return a+b+c
322         return closure_function
323
324 * Native support for new ``buffer`` protocol
325
326   * part of NumPy integration by Dag Seljebotn
327
328   .. sourcecode:: python
329
330     def inplace_negative_grayscale_image(
331                       ndarray[unsigned char, 2] image):
332         cdef int i, j
333         for i in range(image.shape[0]):
334             for j in range(image.shape[1]):
335                 arr[i, j] = 255 - arr[i, j]
336
337
338 Huge pile of great ideas
339 ========================
340
341 * Cython Enhancement Proposals (CEPs)
342
343   * http://wiki.cython.org/enhancements
344
345 * native pickle support for extension classes
346
347 * meta-programming facilities
348
349 * type inference strategies
350
351 * compile-time assertions for optimisations
352
353 * object-like C-array handling
354
355 * improved C++ integration
356
357 * ...
358
359
360 Conclusion
361 ==========
362
363 * Cython is a tool for
364
365   * efficiently translating Python code to C
366
367   * easily interfacing to external C libraries
368
369 * Use it to
370
371   * speed up existing Python modules
372
373     * concentrate on optimisations, not rewrites!
374
375   * write C extensions for CPython
376
377     * don't change the language just to get fast code!
378
379   * wrap C libraries *in Python*
380
381     * concentrate on the mapping, not the glue!
382
383
384 ... but Cython is also
385 ======================
386
387 * a great project
388
389 * a very open playground for great ideas!
390
391
392 Cython
393 ======
394
395   **Cython**
396
397   **C-Extensions in Python**
398
399   \... use it, and join the project!
400
401   http://cython.org/