-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmypyc-quick.html
108 lines (92 loc) · 3.79 KB
/
mypyc-quick.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
<html>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<head>
<title>
leontrolski -
</title>
<style>
body {margin: 5% auto; background: #fff7f7; color: #444444; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.8; max-width: 63%;}
@media screen and (max-width: 800px) {body {font-size: 14px; line-height: 1.4; max-width: 90%;}}
pre {width: 100%; border-top: 3px solid gray; border-bottom: 3px solid gray;}
a {border-bottom: 1px solid #444444; color: #444444; text-decoration: none; text-shadow: 0 1px 0 #ffffff; }
a:hover {border-bottom: 0;}
.inline {background: #b3b2b226; padding-left: 0.3em; padding-right: 0.3em; white-space: nowrap;}
blockquote {font-style: italic;color:black;background-color:#f2f2f2;padding:2em;}
details {border-bottom:solid 5px gray;}
</style>
<link href="https://unpkg.com/[email protected]/themes/prism-vs.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/components/prism-core.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/plugins/autoloader/prism-autoloader.min.js">
</script>
</head>
<body>
<a href="index.html">
<img src="pic.png" style="height:2em">
⇦
</a>
<p><i>2023-02-23</i></p>
<h1>
<code class="inline">mypyc</code>
is quick
</h1>
<p>
<a href="https://mypyc.readthedocs.io/en/latest/">
mypyc
</a>
doesn't get a huge amount of attention, but it's easy to use and can make well-typed Python really fast. No one likes benchmarks, but here's one anyways.
</p>
<p>
Save the following as <code class="inline">lib.py</code>
</p>
<pre class="language-python"><code>from dataclasses import dataclass
@dataclass
class Row:
name: str
age: int
def make() -> list[Row]:
return [Row(name=f"name-{i}", age=i) for i in range(1_000_000)]
def mult(rows: list[Row]) -> None:
for row in rows:
row.name = row.name.upper()
row.age *= 3</code>
</pre>
<p>
And the following as <code class="inline">prof.py</code>
</p>
<pre class="language-python"><code>import time
import lib
before = time.time()
d = lib.make()
print(f"took {time.time() - before:.2f} seconds")
before = time.time()
lib.mult(d)
print(f"took {time.time() - before:.2f} seconds")</code>
</pre>
<p>
Now <code class="inline">python prof.py</code>
</p>
<p>
On my laptop, <code class="inline">make()</code>
runs in 1.95s, <code class="inline">mult()</code>
runs in 0.87s.
</p>
<br>
<p>
Now compile it, <code class="inline">pip install mypy</code>
, <code class="inline">mypyc lib.py</code>
</p>
<p>
On my laptop, <code class="inline">make()</code>
runs in 1.40s, <code class="inline">mult()</code>
runs in 0.03s.
</p>
<br>
<p>
Quick huh!
</p>
<p>
This could be quite an exciting development in the Python ecosystem. Imagine writing interpreted code all day (zero compile times, breakpoints, etc), then compiling for production. Imagine a well-typed and compiled stdlib, flask, SQLAlchemy, etc. It's gonna be great ☀️.
</p>
</body>
</html>