Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Demo: Fast threading with Cython (int overflow issues)
0:00 Now, looking at this timing here
0:02 I think the times are so incredibly small
0:05 that the stuff we're doing around the edges
0:07 is actually starting to show up.
0:08 So, I tweaked things just a little bit.
0:10 First of all, I'm still concerned these
0:12 numbers might overflow, so I switched
0:14 them all to float, okay, float, float, float
0:17 and they're much less likely to do that there.
0:20 Then over here, I said we're taking all this
0:22 time to create the threads, and then
0:25 then, you know, we had this time starting
0:27 like way up here, so, I decided
0:31 like let's create all the threads
0:33 and then start them, and the second
0:35 they're started, start the time, okay?
0:37 Cause, now we're going to wait
0:38 and then we're going to print out the time here.
0:40 Because, I didn't see as much of a change
0:42 between the serial version and the main one.
0:45 So now, now, if we run exactly the same amount
0:49 and we don't do this sort of, especially this stuff
0:52 like getting the processor count, and so on.
0:53 Now, if we run it, now we have a really, really sweet
0:58 performance increase here. Let's actually add one more zero.
1:01 Make that 3,000,000. One final time on this one as well.
1:06 Get our new baseline number. So, 80, I'll put 80 here.
1:13 Now, we can run it, and see what we get.
1:15 Whoops, If you're on the same one
1:16 you get the same number.
1:17 Let's run the Cython line one more time.
1:19 There we go, look at that, 2,000 times faster.
1:23 2,000. 2,200. That is so awesome.
1:27 Let's just make sure that this parallelism
1:29 is having some effect, so what if we go over here
1:31 and we say only use six cores instead of twelve.
1:35 That may make a difference.
1:38 It's a little slow right? Actually, almost half but if we only use two cores?
1:43 Three hundred, three hundred.
1:45 So, you can see the fact that we're doing threading
1:47 It's definitely getting faster and faster
1:49 by using threading, I mean we still have zeros
1:51 all across the board but somewhere in there
1:52 are milliseconds, or microseconds, or something like that
1:55 that we're we're tracking here.
1:57 So, just wanted to, You know, we're going so fast
2:00 that like, the getting the processor count
2:03 and actually allocating the threads
2:05 that really starts to impact our performance, right?
2:08 And even if you say well, we're going to consider
2:11 the starting of the threads, right, it's still
2:13 four hundred times faster than the CPython version.
2:16 Really, really a big deal.
2:18 And I don't have my divide up there, do I still, no.
2:20 Yeah, 416 times faster. 500 that time almost.
2:25 How hard was it? Well, we took the one function right here
2:28 that was a little bit slow, and we're doing
2:30 most of the work, and we converted that to Cython
2:34 but most of our program, all the stuff
2:36 that you've learned about, threading
2:37 the regular methods, while, you know, for loops, everything.
2:42 It's all the same, it's unchanged.
2:43 We can use exactly the same program.
2:46 We just took the one little hotspot
2:48 and replaced it with a Cython implementation.
2:51 It's really nice, isn't it?