Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Cython syntax compared
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
I'm guessing most of you have never written Cython code.
0:03
You might not even know what Cython code looks like.
0:07
Maybe if you saw it you could tell me
0:09
okay, I think that's not Python, it might Cython
0:11
but it turns out the language is slightly
0:14
ever so slightly different. So, let's look at its syntax.
0:16
And here's the example we're going to work with.
0:19
We're going to do some computational stuff here.
0:21
The function is called do_math.
0:23
It takes a start and the number of times
0:25
it's going to increment from there.
0:27
And we've seen this before when we did our threading
0:29
and our multiprocessing.
0:30
It's just a silly math function that does useless math
0:33
but it lets us do performance testing
0:35
and parallelism testing and things like that.
0:37
It's using Python 3.5's type annotations.
0:42
So, up there we have start:, num: and so on
0:44
but of course we could omit those
0:45
and things will still work just the same.
0:47
Right, so this is pure Python, this is not Cython.
0:50
I'm going to show you this function converted
0:53
over to Cython. You ready? Keep your eye on it.
0:57
There's the Cython. There's Python. There's Cython.
1:01
Two changes, one, we have more type annotations
1:05
so start in the argument there is no longer an integer
1:08
it's a Cython int.
1:10
In Python, in CPython, integers and numbers and stuff
1:13
they're still basically objects on the heap.
1:16
They're not allocated on the stack as four bytes
1:18
or eight bytes or whatever the size of these are.
1:21
They're actually allocated in the heap
1:23
and there's pointers, and that really slows down math
1:25
and adds a lot of overhead.
1:26
So we can explicitly say that start
1:28
and num are Cython integers
1:30
and that means they're going to be allocated on the stack in C.
1:34
And then we have three local variables
1:35
which we had before but we didn't explicitly say the type.
1:38
Here we're saying explicitly that's a cython.float.
1:41
cython.float, cython.float.
1:42
And then, we're doing our while loop exactly the same.
1:46
So that's the only change.
1:47
We've done a little bit of type annotations
1:50
and we're using a more high performance square root.
1:53
We were using math.sqrt before
1:55
which is Python's standard library.
1:57
Fine, and that would still work in Cython
1:59
but it turns out to be slower and it prohibits
2:02
one of the techniques we want to apply later.
2:04
So we have this libc.math that actually has sqrt
2:08
and a bunch of other math operations
2:10
that you might want to do.
2:11
So we're going to use Cython's built-in
2:14
more high performance math operation. That's it.
2:18
I'll flip back and forth a couple times for you
2:20
one more time.
2:21
Just notice, just a few more type decorations
2:23
and a different square root. Pretty sweet, right?