forked from serge-sans-paille/pythran-stories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting-pythran-on-random-kernels.html
462 lines (384 loc) · 54.6 KB
/
testing-pythran-on-random-kernels.html
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Testing Pythran on random kernels</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="serge-sans-paille and other pythraners">
<!-- Le styles -->
<link rel="stylesheet" href="./theme/css/bootstrap.min.css" type="text/css" />
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
.sidebar-nav {
padding: 9px 0;
}
.tag-1 {
font-size: 13pt;
}
.tag-2 {
font-size: 10pt;
}
.tag-2 {
font-size: 8pt;
}
.tag-4 {
font-size: 6pt;
}
</style>
<link href="./theme/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="./theme/css/font-awesome.css" rel="stylesheet">
<link href="./theme/css/pygments.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="./theme/images/favicon.ico">
<link rel="apple-touch-icon" href="./theme/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="./theme/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="./theme/images/apple-touch-icon-114x114.png">
<link href="./" type="application/atom+xml" rel="alternate" title="Pythran stories ATOM Feed" />
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="./index.html">Pythran stories </a>
<div class="nav-collapse">
<ul class="nav">
<li class="divider-vertical"></li>
<li >
<a href="./category/benchmark.html">
<i class="icon-folder-open icon-large"></i>benchmark
</a>
</li>
<li >
<a href="./category/compilation.html">
<i class="icon-folder-open icon-large"></i>compilation
</a>
</li>
<li >
<a href="./category/cython.html">
<i class="icon-folder-open icon-large"></i>cython
</a>
</li>
<li >
<a href="./category/engineering.html">
<i class="icon-folder-open icon-large"></i>engineering
</a>
</li>
<li class="active">
<a href="./category/examples.html">
<i class="icon-folder-open icon-large"></i>examples
</a>
</li>
<li >
<a href="./category/optimisation.html">
<i class="icon-folder-open icon-large"></i>optimisation
</a>
</li>
<li >
<a href="./category/release.html">
<i class="icon-folder-open icon-large"></i>release
</a>
</li>
<ul class="nav pull-right">
<li><a href="./archives.html"><i class="icon-th-list"></i>Archives</a></li>
</ul>
</ul>
<!--<p class="navbar-text pull-right">Logged in as <a href="#">username</a></p>-->
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="span9" id="content">
<section id="content">
<article>
<header>
<h1>
<a href=""
rel="bookmark"
title="Permalink to Testing Pythran on random kernels">
Testing Pythran on random kernels
</a>
</h1>
</header>
<div class="entry-content">
<div class="well">
<footer class="post-info">
<span class="label">Date</span>
<abbr class="published" title="2018-07-11T00:00:00+02:00">
<i class="icon-calendar"></i>Wed 11 July 2018
</abbr>
<span class="label">By</span>
<a href="./author/serge-sans-paille.html"><i class="icon-user"></i>serge-sans-paille</a>
<span class="label">Category</span>
<a href="./category/examples.html"><i class="icon-folder-open"></i>examples</a>.
</footer><!-- /.post-info --> </div>
<p>This blogpost originally was a Jupyter Notebook. You can <a href="notebooks/Testing Pythran on random kernels.ipynb">download it</a> if you want. The conversion was done using <code>nbconvert</code> and a <a href="notebooks/nbmarkdown.tpl">custom template</a> to match the style of the other part of the blog.</p>
<p>Every now and then, I hang around on stackoverflow, longing for numerical kernels to pass through <a href="https://pythran.readthedocs.io">pythran</a>. Here is the result of my recent wanderings :-)</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="c1"># It's import(ant)</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">pythran</span><span class="o">,</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="o">%</span><span class="n">load_ext</span> <span class="n">pythran</span><span class="o">.</span><span class="n">magic</span>
</pre></div>
<h1>From stackoverflow</h1>
<h2>euclidian distance</h2>
<p>from <a href="https://stackoverflow.com/questions/50658884/why-this-numba-code-is-6x-slower-than-numpy-code">https://stackoverflow.com/questions/50658884/why-this-numba-code-is-6x-slower-than-numpy-code</a> . This kernel is interesting because it uses <code>np.newaxis</code>, <code>np.sum</code>) along an axis, and a matrix against transposed matrix dot product.</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">euclidean_distance_square</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">):</span>
<span class="o">...</span> <span class="k">return</span> <span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">x1</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">x2</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">pythran</span>
<span class="o">>>></span> <span class="c1">#pythran export pythran_euclidean_distance_square(float64[1,:], float64[:,:])</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">pythran_euclidean_distance_square</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">):</span>
<span class="o">...</span> <span class="k">return</span> <span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">x1</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">x2</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="n">x1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">512</span><span class="p">))</span>
<span class="o">>>></span> <span class="n">x2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">((</span><span class="mi">10000</span><span class="p">,</span> <span class="mi">512</span><span class="p">))</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">euclidean_distance_square</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">)</span>
<span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">pythran_euclidean_distance_square</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span>16.1 ms ± 905 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
11.1 ms ± 76.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
</pre></div>
<p>As a side note, at some point in history, pythran failed to match the <code>np.dot(x1, x2.T)</code> pattern, but it now calls the appropriate blas API (<code>cblas_zgemm</code>) with the correct arguments, without copy.</p>
<h2>Updated centers</h2>
<p>from <a href="https://stackoverflow.com/questions/50931002/cython-numpy-array-manipulation-slower-than-python/50964759">https://stackoverflow.com/questions/50931002/cython-numpy-array-manipulation-slower-than-python/50964759</a>. This is a funny kernel because of its use of list comprehension.</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">updated_centers</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">center</span><span class="p">):</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">__cluster_mean</span><span class="p">(</span><span class="n">point</span><span class="p">[</span><span class="n">start</span><span class="p">[</span><span class="n">c</span><span class="p">]:</span><span class="n">start</span><span class="p">[</span><span class="n">c</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]],</span> <span class="n">center</span><span class="p">[</span><span class="n">c</span><span class="p">])</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">center</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])])</span>
<span class="o">...</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">__cluster_mean</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">center</span><span class="p">):</span>
<span class="o">...</span> <span class="k">return</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="n">center</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">point</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">pythran</span>
<span class="o">>>></span> <span class="c1">#pythran export pythran_updated_centers(float64 [:, :], intc[:] , float64 [:, :] )</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">pythran_updated_centers</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">center</span><span class="p">):</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">__cluster_mean</span><span class="p">(</span><span class="n">point</span><span class="p">[</span><span class="n">start</span><span class="p">[</span><span class="n">c</span><span class="p">]:</span><span class="n">start</span><span class="p">[</span><span class="n">c</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]],</span> <span class="n">center</span><span class="p">[</span><span class="n">c</span><span class="p">])</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">center</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])])</span>
<span class="o">...</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">__cluster_mean</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">center</span><span class="p">):</span>
<span class="o">...</span> <span class="k">return</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="n">center</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">point</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span> <span class="o">=</span> <span class="mi">100000</span><span class="p">,</span> <span class="mi">5</span>
<span class="o">>>></span> <span class="n">k</span> <span class="o">=</span> <span class="n">n</span><span class="o">//</span><span class="mi">2</span>
<span class="o">>>></span> <span class="n">point</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">start</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">center</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">m</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">updated_centers</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">center</span><span class="p">)</span>
<span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">pythran_updated_centers</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">center</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span>295 ms ± 18.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
11.9 ms ± 71.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
</pre></div>
<p>That's a cool speedup, but that's normal: there is an explicit loop + array indexing pattern, and that's not where numpy shines.</p>
<h2>Gaussian Process</h2>
<p>from <a href="https://stackoverflow.com/questions/46334298/kernel-function-in-gaussian-processes">https://stackoverflow.com/questions/46334298/kernel-function-in-gaussian-processes</a>, a very high level kernel. The pythran version uses indexing through <code>np.newaxis</code> instead of the reshaping, and generates a specialized version for arguments where the last dimension is known to be one. There's two version of the pythran kernel, compiled with different flags to showcase the effect of vectorization.</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">gp</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">gamma</span><span class="o">=</span><span class="mf">0.1</span><span class="p">):</span>
<span class="o">...</span> <span class="s2">""" GP squared exponential kernel """</span>
<span class="o">...</span> <span class="n">sq_dist</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">b</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">gamma</span><span class="p">)</span> <span class="o">*</span> <span class="n">sq_dist</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">pythran</span>
<span class="o">>>></span> <span class="c1">#pythran export pythran_gp_novect(float64[:,1], float64[:,1])</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">pythran_gp_novect</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">gamma</span><span class="o">=</span><span class="mf">0.1</span><span class="p">):</span>
<span class="o">...</span> <span class="s2">""" GP squared exponential kernel """</span>
<span class="o">...</span> <span class="n">sq_dist</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">b</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">gamma</span><span class="p">)</span> <span class="o">*</span> <span class="n">sq_dist</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">pythran</span> <span class="o">-</span><span class="n">DUSE_BOOST_SIMD</span> <span class="o">-</span><span class="n">march</span><span class="o">=</span><span class="n">native</span>
<span class="o">>>></span> <span class="c1">#pythran export pythran_gp_vect(float64[:,1], float64[:,1])</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">pythran_gp_vect</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">gamma</span><span class="o">=</span><span class="mf">0.1</span><span class="p">):</span>
<span class="o">...</span> <span class="s2">""" GP squared exponential kernel """</span>
<span class="o">...</span> <span class="n">sq_dist</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">a</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">b</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">gamma</span><span class="p">)</span> <span class="o">*</span> <span class="n">sq_dist</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="n">n</span> <span class="o">=</span> <span class="mi">300</span>
<span class="o">>>></span> <span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">gp</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">X</span><span class="p">)</span>
<span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">pythran_gp_novect</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">X</span><span class="p">)</span>
<span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">pythran_gp_vect</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">X</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span>1.51 ms ± 6.73 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
1.21 ms ± 6.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
348 µs ± 20.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
</pre></div>
<p>Not that much of a gain without vectorization enable, but still Pythran can rip a few extra performance out of a very high level kernel. Unleashing vectorization is plain awesome here :-)</p>
<h2>Image processing</h2>
<p>from <a href="https://stackoverflow.com/questions/45714178/python-improving-image-processing-with-numpy">https://stackoverflow.com/questions/45714178/python-improving-image-processing-with-numpy</a>, that's the kind of kernel where an explicit seems a natural fit, and where pythran shines.</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="k">def</span> <span class="nf">image_processing</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">sum_arr</span><span class="p">):</span> <span class="c1"># Proposed approach</span>
<span class="o">...</span> <span class="n">B_ext</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">B</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="n">B</span><span class="p">))</span>
<span class="o">...</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">A</span><span class="p">)</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span>
<span class="o">...</span> <span class="n">A</span> <span class="o">*=</span> <span class="n">B_ext</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span><span class="o">+</span><span class="n">n</span><span class="p">]</span> <span class="c1">#roll B with i-increment and multiply</span>
<span class="o">...</span> <span class="n">A</span><span class="p">[</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="o">-</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sum_arr</span> <span class="c1">#add sum to A at index</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">A</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">pythran</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="c1">#pythran export pythran_image_processing(int64[], int64[], int64)</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">pythran_image_processing</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">sum_arr</span><span class="p">):</span> <span class="c1"># Proposed approach</span>
<span class="o">...</span> <span class="n">B_ext</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">B</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="n">B</span><span class="p">))</span>
<span class="o">...</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">A</span><span class="p">)</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span>
<span class="o">...</span> <span class="n">A</span> <span class="o">*=</span> <span class="n">B_ext</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span><span class="o">+</span><span class="n">n</span><span class="p">]</span> <span class="c1">#roll B with i-increment and multiply</span>
<span class="o">...</span> <span class="n">A</span><span class="p">[</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="o">-</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="n">sum_arr</span> <span class="c1">#add sum to A at index</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">A</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">N</span> <span class="o">=</span> <span class="mi">10000</span>
<span class="o">>>></span> <span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,(</span><span class="n">N</span><span class="p">))</span>
<span class="o">>>></span> <span class="n">B</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,(</span><span class="n">N</span><span class="p">))</span>
<span class="o">>>></span> <span class="n">A_copy</span> <span class="o">=</span> <span class="n">A</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">sum_arr</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">B</span><span class="p">))</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">image_processing</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">sum_arr</span><span class="p">)</span>
<span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">pythran_image_processing</span><span class="p">(</span><span class="n">A_copy</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">sum_arr</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span>60 ms ± 2.09 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
51.7 ms ± 2.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
</pre></div>
<h2>Lorenz Attractor</h2>
<p>from <a href="https://gist.github.com/dean-shaff/d1d0cdabf79e225ab96918b73916289f">https://gist.github.com/dean-shaff/d1d0cdabf79e225ab96918b73916289f</a>. yet another kernel with loops, but sometimes that's the way the problem is naturally expressed. Note that Pythran does not support start arguments yet :-/</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">rungekuttastep</span><span class="p">(</span><span class="n">h</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">fprime</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="o">...</span> <span class="n">k1</span> <span class="o">=</span> <span class="n">h</span><span class="o">*</span><span class="n">fprime</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
<span class="o">...</span> <span class="n">k2</span> <span class="o">=</span> <span class="n">h</span><span class="o">*</span><span class="n">fprime</span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="mf">0.5</span><span class="o">*</span><span class="n">k1</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
<span class="o">...</span> <span class="n">k3</span> <span class="o">=</span> <span class="n">h</span><span class="o">*</span><span class="n">fprime</span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="mf">0.5</span><span class="o">*</span><span class="n">k2</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
<span class="o">...</span> <span class="n">k4</span> <span class="o">=</span> <span class="n">h</span><span class="o">*</span><span class="n">fprime</span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="n">k3</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
<span class="o">...</span> <span class="n">y_np1</span> <span class="o">=</span> <span class="n">y</span> <span class="o">+</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mf">6.</span><span class="p">)</span><span class="o">*</span><span class="n">k1</span> <span class="o">+</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mf">3.</span><span class="p">)</span><span class="o">*</span><span class="n">k2</span> <span class="o">+</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mf">3.</span><span class="p">)</span><span class="o">*</span><span class="n">k3</span> <span class="o">+</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mf">6.</span><span class="p">)</span><span class="o">*</span><span class="n">k4</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">y_np1</span>
<span class="o">...</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">fprime_lorenz_numpy</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="o">...</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span> <span class="o">=</span> <span class="n">args</span>
<span class="o">...</span> <span class="n">yprime</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="o">...</span> <span class="n">yprime</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">sigma</span><span class="o">*</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="o">...</span> <span class="n">yprime</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">rho</span> <span class="o">-</span> <span class="n">y</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">-</span> <span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="o">...</span> <span class="n">yprime</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">beta</span><span class="o">*</span><span class="n">y</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">yprime</span>
<span class="o">...</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">attractor</span><span class="p">(</span><span class="n">n_iter</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">):</span>
<span class="o">...</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">n_iter</span><span class="p">):</span>
<span class="o">...</span> <span class="n">y</span> <span class="o">=</span> <span class="n">rungekuttastep</span><span class="p">(</span><span class="mf">0.001</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">fprime_lorenz_numpy</span><span class="p">,</span><span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">y</span>
<span class="o">...</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">pythran</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">rungekuttastep</span><span class="p">(</span><span class="n">h</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">fprime</span><span class="p">,</span><span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">):</span>
<span class="o">...</span> <span class="n">k1</span> <span class="o">=</span> <span class="n">h</span><span class="o">*</span><span class="n">fprime</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span>
<span class="o">...</span> <span class="n">k2</span> <span class="o">=</span> <span class="n">h</span><span class="o">*</span><span class="n">fprime</span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="mf">0.5</span><span class="o">*</span><span class="n">k1</span><span class="p">,</span><span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span>
<span class="o">...</span> <span class="n">k3</span> <span class="o">=</span> <span class="n">h</span><span class="o">*</span><span class="n">fprime</span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="mf">0.5</span><span class="o">*</span><span class="n">k2</span><span class="p">,</span><span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span>
<span class="o">...</span> <span class="n">k4</span> <span class="o">=</span> <span class="n">h</span><span class="o">*</span><span class="n">fprime</span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="n">k3</span><span class="p">,</span><span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span>
<span class="o">...</span> <span class="n">y_np1</span> <span class="o">=</span> <span class="n">y</span> <span class="o">+</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mf">6.</span><span class="p">)</span><span class="o">*</span><span class="n">k1</span> <span class="o">+</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mf">3.</span><span class="p">)</span><span class="o">*</span><span class="n">k2</span> <span class="o">+</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mf">3.</span><span class="p">)</span><span class="o">*</span><span class="n">k3</span> <span class="o">+</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mf">6.</span><span class="p">)</span><span class="o">*</span><span class="n">k4</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">y_np1</span>
<span class="o">...</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">fprime_lorenz_numpy</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">):</span>
<span class="o">...</span> <span class="n">yprime</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="o">...</span> <span class="n">yprime</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">sigma</span><span class="o">*</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="o">...</span> <span class="n">yprime</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">rho</span> <span class="o">-</span> <span class="n">y</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">-</span> <span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="o">...</span> <span class="n">yprime</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">beta</span><span class="o">*</span><span class="n">y</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">yprime</span>
<span class="o">...</span>
<span class="o">>>></span> <span class="c1">#pythran export pythran_attractor(int, float, float, float)</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">pythran_attractor</span><span class="p">(</span><span class="n">n_iter</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">):</span>
<span class="o">...</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">n_iter</span><span class="p">):</span>
<span class="o">...</span> <span class="n">y</span> <span class="o">=</span> <span class="n">rungekuttastep</span><span class="p">(</span><span class="mf">0.001</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">fprime_lorenz_numpy</span><span class="p">,</span><span class="n">sigma</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">y</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">attractor</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mf">10.</span><span class="p">,</span><span class="mf">28.</span><span class="p">,</span><span class="mf">8.</span><span class="o">/</span><span class="mf">3.</span><span class="p">)</span>
<span class="o">>>></span> <span class="o">%</span><span class="n">timeit</span> <span class="n">pythran_attractor</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mf">10.</span><span class="p">,</span><span class="mf">28.</span><span class="p">,</span><span class="mf">8.</span><span class="o">/</span><span class="mf">3.</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span>17 ms ± 68.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
682 µs ± 8.62 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
</pre></div>
<p>Again, that's a lot of non-vectorized operation, not the best fit for numpy but that's okay for pythran. There's a function passed as a parameter of another function, but pythran can cope with that.</p>
</div><!-- /.entry-content -->
</article>
</section>
</div><!--/span-->
<div class="span3 well sidebar-nav" id="sidebar">
<ul class="nav nav-list">
<li class="nav-header"><h4><i class="icon-external-link"></i>blogroll</h4></li>
<li><a href="http://pythonhosted.org/pythran"><i class="icon-external-link"></i>Pythran Doc</a></li>
<li><a href="https://pypi.python.org/pypi/pythran"><i class="icon-external-link"></i>Pythran on PyPI</a></li>
<li class="nav-header"><h4><i class="icon-home icon-large"></i> social</h4></li>
<li><a href="./feeds/all.atom.xml" rel="alternate"><i class="icon-bookmark icon-large"></i>atom feed</a></li>
<li><a href="https://github.com/serge-sans-paille/pythran"><i class="icon-github-sign icon-large"></i>github</a></li>
<li class="nav-header"><h4><i class="icon-folder-close icon-large"></i>Categories</h4></li>
<li>
<a href="./category/benchmark.html">
<i class="icon-folder-open icon-large"></i>benchmark
</a>
</li>
<li>
<a href="./category/compilation.html">
<i class="icon-folder-open icon-large"></i>compilation
</a>
</li>
<li>
<a href="./category/cython.html">
<i class="icon-folder-open icon-large"></i>cython
</a>
</li>
<li>
<a href="./category/engineering.html">
<i class="icon-folder-open icon-large"></i>engineering
</a>
</li>
<li>
<a href="./category/examples.html">
<i class="icon-folder-open icon-large"></i>examples
</a>
</li>
<li>
<a href="./category/optimisation.html">
<i class="icon-folder-open icon-large"></i>optimisation
</a>
</li>
<li>
<a href="./category/release.html">
<i class="icon-folder-open icon-large"></i>release
</a>
</li>
<li class="nav-header"><h4><i class="icon-tags icon-large"></i>Tags</h4></li>
</ul> </div><!--/.well -->
</div><!--/row-->
<hr>
<footer>
<address id="about">
Proudly powered by <a href="http://pelican.notmyidea.org/">Pelican <i class="icon-external-link"></i></a>,
which takes great advantage of <a href="http://python.org">Python <i class="icon-external-link"></i></a>.
</address><!-- /#about -->
<p>The theme is from <a href="http://twitter.github.com/bootstrap/">Bootstrap from Twitter <i class="icon-external-link"></i></a>,
and <a href="http://fortawesome.github.com/Font-Awesome/">Font-Awesome <i class="icon-external-link"></i></a>, thanks!</p>
</footer>
</div><!--/.fluid-container-->
<!-- Le javascript -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="./theme/js/jquery-1.7.2.min.js"></script>
<script src="./theme/js/bootstrap.min.js"></script>
</body>
</html>