Python for the .NET developer Transcripts
Chapter: Web frameworks
Lecture: ASP.NET and Flask performance compared

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Earlier, I talked a little bit about
0:02 performance, and that Python is actually
0:04 an incredible framework or language
0:06 to write web applications in.
0:09 I said, Look, it may seem like
0:11 .NET is incredibly fast. It compiles to IL
0:14 which it compiles to machine instructions.
0:16 How could it possibly
0:18 not just blow Python out of the water?
0:20 And there's certain operations
0:21 where .NET absolutely does
0:23 especially numerical computation
0:25 if, big if, if you're not using the scientific
0:28 libraries in Python
0:29 but you're just trying to do straight math.
0:31 C# is way faster, no doubt.
0:33 But that probably doesn't matter to you.
0:34 You probably just want your web app to go super fast.
0:37 The folks over at Microsoft have done
0:39 a ton of work to make web processing
0:42 way faster in .NET.
0:44 Back in the a speed .NET web form days
0:46 where it was regular .NET, it was so much
0:49 data being created and passed around.
0:50 It was terribly slow.
0:52 They've done some really good work.
0:53 And now, if we go and hit local host, guitar
0:56 /electric with the right port on it
0:58 and we do this a bunch of times
1:00 you will see that the .NET Core 3 version
1:03 which is what we are running
1:04 the C# one. It'll blaze.
1:06 It will do that whole thing
1:08 that non database driven one
1:10 in 3.5 milliseconds per request.
1:12 Or it will do a 1000 requests
1:15 in 3.5 second for example.
1:18 That is really good right.
1:19 Would you be surprised to hear that Python
1:21 does the same thing at 2.6 milliseconds?
1:23 Yeah, not only is it not that bad it's faster.
1:28 I don't even understand how it is faster.
1:30 It shouldn't be faster than the compiled
1:33 stuff that's happening in C# side.
1:35 But it is. So I think this is pretty awesome.
1:38 Going to show you a little program that you
1:40 can use to check this for yourself.
1:43 Over here, in ch_07, I have now added
1:45 the speed_test. The way it works doesn't really matter.
1:48 You have to have it in your requirement
1:50 which I've added here in requests.
1:51 But what we are going to do
1:53 is going to run this against our website.
1:54 Now. Also, another quick note
1:56 about running these websites here.
1:58 See how this one is marked as a
2:00 sorcerer's blue and this one is not.
2:03 Because these have the same stuff in them
2:06 if you mark them both as it, the first one
2:08 is going to be where the actual
2:10 implementation comes from.
2:11 So if you want to run this one
2:12 you got to unmark, right click, um
2:15 mark directory as unmark as sources root.
2:17 You can't have them both to set
2:18 to run at the same time.
2:19 It's kind of a unfortunate thing.
2:22 Very rarely you will actually have
2:23 these together like this except for demo code
2:25 which of coarse this is. But let met go run
2:30 this app first. That was going to be running here.
2:34 Make sure it works. Looks like a champ. Okay.
2:37 Here is the scope of the electric guitars.
2:40 Here is what we are going to get.
2:43 Now that is running.
2:44 Now let's also go run this speed test down here.
2:47 Make sure you give a space here or
2:50 a Python is weird and I'm trying to open it.
2:53 Let's say the URL is that.
2:54 Let's do this 100 times. Go request that.
2:57 Now remember, this is the Python version.
2:59 Boom. 2.98 milliseconds. Run it again.
3:06 Ah, there is that. Don't forget the space.
3:09 End times, 4 seconds this time.
3:11 Better run it a little bit more I guess.
3:17 There we go. 2.9. Pretty good.
3:20 We got a lot going on with screen recording and stuff
3:22 so it's probably slowing it down a little bit.
3:23 But nonetheless, that is blazing fast.
3:26 That is faster than .NET.
3:29 Doing quite a bit too. Remember it's doing it's little query
3:31 it's doing it's filter, returning stuff.
3:33 It's probably even faster if we don't
3:35 ask for the electric, but we ask for all.
3:41 And it is about the same.
3:42 It doesn't really matter does it.
3:43 But nonetheless, its pretty impressive.
3:46 Yeah, I think it is a little messed up.
3:49 I don't understand what it's doing there.
3:51 Anyway, it's really cool how quick and fast this is.
3:53 And you can use this speed test app to test
3:56 any website, right?
3:57 You can put that against Google if you like.
4:00 You can try it on my site. 10 times.
4:08 Pretty good. Now, why is it so slow?
4:10 It's so slow because it's far away
4:11 across the internet. But whatever.
4:13 You know, you can't help with PING time.
4:14 Nonetheless, it's pretty cool you can test your app
4:17 locally with that. Oh, one more thing!
4:21 I did forget. I did forget.
4:23 Uh, this cannot be helping.
4:24 Sorry, let's turn that off.
4:25 I think I had turned that off as well.
4:30 Let's try again. Ah, there we go!
4:33 There's that 2.5 I was looking at.
4:34 We had the debug mode and so a lot of extra
4:37 stuff was slowing down in there.
4:39 Perfect! Finally, you might be thinking, okay
4:43 Python is faster here, but that's because
4:44 it's like this fake little app.
4:47 I think it's doing quite a bit in there
4:49 for a standard web app.
4:50 But nonetheless, maybe it's
4:51 maybe it is doing more.
4:53 So let me show you real output from
4:55 a real production server, that is doing
4:57 way, way, way, more.
4:59 Here is a very small segment of the logs
5:03 on training.talkpython.fm
5:05 and these pages do a ridiculous amount
5:07 of database queries.
5:09 Got to figure out what the user is.
5:10 Got to go back to get a bunch of courses.
5:12 Got to figure out if you finished watching these courses.
5:15 All kinds of stuff as you are you interacting
5:16 with it once have you marked it finished.
5:19 What are your preferences?
5:20 Do you want the video to play at 1.25 speed?
5:23 So there are a ton of stuff happening here.
5:24 Look at some of these numbers.
5:26 Yeah, some of these are like a hundred milliseconds.
5:28 And you know, either thats because the server
5:31 hasn't really hit that page very often
5:33 or like the entrepreneurs one, just a huge huge page.
5:36 So it just takes a little bit of time to generate it.
5:38 But look at the stuff on the bottom.
5:40 My course summaries. Get the free courses.
5:43 Or this is probably the api, that the app
5:45 the mobile app your using. 11 seconds
5:49 7 milliseconds, 10 milliseconds, 46
5:53 for log in 6 milliseconds
5:55 to render the log in page.
5:56 or to render the account page.
5:57 25 milliseconds. This is real stuff.
6:00 In production. On linux.
6:02 On a pretty small and cheap server.
6:04 Right, these are pretty solid numbers folks.
6:07 Given the amount of net back and fourth with the database
6:10 and other types of things that we are doing here.