Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Demo: Fast threading with Cython (hotspot)
0:00 Now, we're going to take this program
0:01 and make it faster by using Cython.
0:04 And we saw in the threaded version
0:06 we're calling do_math
0:07 and that's not actually helping.
0:09 But we did try to apply threading
0:12 to the do_math operation and break it into segments
0:14 to see if that would help.
0:15 And with Cython it will.
0:17 But let's just take a step back for a moment
0:19 before we get into all that and say
0:21 "How do I know where my program is slow?"
0:23 If you're going to rewrite some part in Cython
0:26 don't do the whole thing
0:27 just pick the one little part that really needs attention.
0:29 How do you know where that is?
0:31 Well, if you have Pycharm Professional
0:33 you can run a cool in IDE tool.
0:37 If you don't, you can go look into CProfile
0:40 and see how to actually to that in Python
0:42 on the command line.
0:43 But I'll show you how to just quickly do it
0:45 in Pycharm Pro.
0:46 Come over here and you say profile
0:48 this is not in our parallel one
0:50 this is just the plain one.
0:51 Let's ask, where is it spending it's time?
0:53 Well, here it shows you all the operations it does
0:56 but check out the graph.
0:58 Does anything stand out there?
0:59 Is something maybe a little red?
1:01 Let's go look and see what's going on there.
1:03 Well look at that, we're spending 1.056 seconds
1:08 on computed, and of all that time
1:11 all but 2 milliseconds are being spent
1:13 in main, all of those 1054 milliseconds
1:16 are being spent in do_math.
1:18 So if we're going to make our code fast
1:20 we don't need to make everything fast
1:22 it's just this one. Right?
1:24 So if we navigate to source, well
1:26 that's the function we knew we we're working with
1:27 that's where it's going to be slow.
1:29 In a real program, this is the type of thinking
1:32 that you can go through. Show me where it's slow.
1:34 What's the worst case scenario?
1:36 Ask yourself the question
1:37 "Could this be fixed by switching to Cython?"