Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Demo: Fast threading with Cython (hotspot)

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, we're going to take this program and make it faster by using Cython. And we saw in the threaded version we're calling do_math
0:08 and that's not actually helping. But we did try to apply threading to the do_math operation and break it into segments to see if that would help.
0:16 And with Cython it will. But let's just take a step back for a moment before we get into all that and say "How do I know where my program is slow?"
0:24 If you're going to rewrite some part in Cython don't do the whole thing just pick the one little part that really needs attention.
0:30 How do you know where that is? Well, if you have Pycharm Professional you can run a cool in IDE tool. If you don't, you can go look into CProfile
0:41 and see how to actually to that in Python on the command line. But I'll show you how to just quickly do it in Pycharm Pro.
0:47 Come over here and you say profile this is not in our parallel one this is just the plain one. Let's ask, where is it spending it's time?
0:54 Well, here it shows you all the operations it does but check out the graph. Does anything stand out there? Is something maybe a little red?
1:02 Let's go look and see what's going on there. Well look at that, we're spending 1.056 seconds on computed, and of all that time
1:12 all but 2 milliseconds are being spent in main, all of those 1054 milliseconds are being spent in do_math. So if we're going to make our code fast
1:21 we don't need to make everything fast it's just this one. Right? So if we navigate to source, well that's the function we knew we we're working with
1:28 that's where it's going to be slow. In a real program, this is the type of thinking that you can go through. Show me where it's slow.
1:35 What's the worst case scenario? Ask yourself the question "Could this be fixed by switching to Cython?"


Talk Python's Mastodon Michael Kennedy's Mastodon