|
| 1 | +00:00 Let's look at a real world example |
| 2 | +00:01 of a Python programming that is doing a very poor job |
| 3 | +00:05 taking advantage of the CPU it's running on. |
| 4 | +00:08 I'm running on my MacBook that I discussed |
| 5 | +00:10 in the opening, has 12 cores. |
| 6 | +00:13 This is the Intel i9 12 core maxed out |
| 7 | +00:18 you know, all nobs to 11 type of CPU |
| 8 | +00:20 for my MacBook Pro, here, so you can see |
| 9 | +00:22 it has a lot of processors. It has a lot of cores. |
| 10 | +00:27 In particular, has 12 hyper-threaded cores |
| 11 | +00:29 so six real cores, each one of which is hyper-threaded. |
| 12 | +00:34 Here we have a program running in the background |
| 13 | +00:35 working as hard as it can. |
| 14 | +00:37 How much CPU is the entire computer using? Well, 9.5%. |
| 15 | +00:43 If this were 10 years ago, on single core systems |
| 16 | +00:46 the CPU usage would be 100% but you can see |
| 17 | +00:49 most of those cores are dark, they're doing nothing |
| 18 | +00:53 they're just sitting there. |
| 19 | +00:54 That's because our code is written |
| 20 | +00:56 in a single-threaded way. |
| 21 | +00:58 It can't go any faster. |
| 22 | +00:59 Let's just look at that in action real quick. |
| 23 | +01:01 Now, this graph, this cool graph |
| 24 | +01:03 showing the 12 CPUs and stuff |
| 25 | +01:05 this comes from a program called Process Explorer |
| 26 | +01:09 which is a free program for Windows. |
| 27 | +01:10 I'm on my Mac, so I'll show you something |
| 28 | +01:12 not nearly as good but I really wanted to show you |
| 29 | +01:14 this graph because I think it brings home |
| 30 | +01:16 just how underutilized this system is. |
| 31 | +01:19 So let's go over here and we're going to run |
| 32 | +01:22 a performance or system monitoring tool |
| 33 | +01:25 actually built in Python |
| 34 | +01:26 which is pretty cool in and of itself, called Glances. |
| 35 | +01:29 So we're sorting by CPU usage, we're showing what's going on. |
| 36 | +01:33 We had a little Python running there, actually |
| 37 | +01:35 for a second, running probably in PyCharm |
| 38 | +01:37 which is just hanging out, but you can see the system |
| 39 | +01:39 is not really doing a whole lot, it is recording. |
| 40 | +01:41 Camtasia's doing a lot of work to record the screen |
| 41 | +01:43 so you have to sort of factor that out. |
| 42 | +01:46 Now, let's go over here and create another screen |
| 43 | +01:50 notice right here we have 12 cores. |
| 44 | +01:52 Okay, so if we run PtPython, which is a nicer REPL |
| 45 | +01:57 but just Python, and we write an incredibly simple program |
| 46 | +02:00 that's going to just hammer the CPU. |
| 47 | +02:03 Say x = 1, and then while true: x += 1. |
| 48 | +02:09 So we're just going to increment this, over and over and over. |
| 49 | +02:12 I'm going to hit go, you should see Python jump up here |
| 50 | +02:16 and just consume 100% of one core. |
| 51 | +02:19 This column here is actually measuring in |
| 52 | +02:24 single core consumption whereas this |
| 53 | +02:27 is the overall 12 core bit. |
| 54 | +02:31 Here it's working as hard as it can. |
| 55 | +02:33 You'll see Python'll jump up there in a second. |
| 56 | +02:37 Okay, so Python is working as hard as it can, 99.8% |
| 57 | +02:41 running out of my brew installed Python, there. |
| 58 | +02:44 But the overall CP usage, including screen recording |
| 59 | +02:48 on this whole system? |
| 60 | +02:50 11%, and of course as we discussed, that's because our code |
| 61 | +02:53 we wrote in the REPL, it only uses |
| 62 | +02:55 one thread, only one thread of concurrent execution |
| 63 | +02:59 that means the best we could ever possibly get is 1/12%. |
| 64 | +03:04 8.3% is the best we could ever do |
| 65 | +03:07 in terms of taking advantage of this computer |
| 66 | +03:10 unless we leverage the async capabilities |
| 67 | +03:12 that we're going to discuss in this course. |
| 68 | +03:14 So if you want to take advantage of modern hardware |
| 69 | +03:17 with multiple cores, the more cores the more demanding |
| 70 | +03:21 or more pressing this desire is |
| 71 | +03:24 you have to write concurrent multi-threaded code. |
| 72 | +03:27 Of course, we're going to see a variety of ways to do this |
| 73 | +03:29 both for I/O bound and, like this, CPU bound work |
| 74 | +03:33 and you handle those differently in Python |
| 75 | +03:35 which is not always true for other languages |
| 76 | +03:37 but it is true for Python. |
| 77 | +03:38 And by the end of this course, you'll know |
| 78 | +03:41 several ways to make, maybe not this simple, simple program |
| 79 | +03:45 but to make your Python program doing real computation |
| 80 | +03:48 push that up to nearly 100% so it's fully taking advantage |
| 81 | +03:52 of the CPU capabilities available to it. |
0 commit comments