#100DaysOfCode in Python Transcripts
Chapter: Days 49-51: Measuring performance
Lecture: A quantum warning

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, before I turn you loose to go work on the code on your own and do your own profiling, I want to give you a short warning.
0:09 It's not a major warning, but there is an effect you really need to be aware of. There's some parallels here with quantum mechanics;
0:16 in quantum mechanics we have Heisenberg's uncertainty principle where it talks about the more precisely you know
0:22 the position of something, the less precisely you can tell its speed and momentum. So, by measuring one thing really closely
0:29 you have actually weakened your understanding of the other. And profilers are like this as well. You have your code operating one way
0:37 and you measure it really deeply, actually put tons of hooks into it to do this monitoring or porting with cProfile
0:45 and it might actually change the performance of your code. So, when you look at those numbers, like for example we said our program takes
0:51 610 milliseconds to run. Maybe it only takes 400 milliseconds to run, but with profiling it takes 600 and these effects are not evenly distributed.
1:00 It's not like, "Well, the whole program slows down 20%." No. Some of it is barely affected. Some of it's massively affected
1:07 and I would say a general rule is: the more things you do in small pieces, that's affected more.
1:15 The stuff that's kind of pushed into the standard library, it doesn't have it's hooks in there. So, for example that parse row loop
1:22 looping over every row in CSV probably has some affect where as the sort, that's one line measured and then it's handed off to CPython
1:32 and then it's time when it's back. So, there might be a ton of operations in there but they're not measured.
1:37 So, just be aware this Heisenberg uncertainty principle going on with profilers also somewhat applies to debuggers. So, they kind of both apply here
1:46 but in this context we're concerned about profilers and timing and it really can have a big affect. That said, these profilers are massively helpful
1:55 and much better than your intuition on understanding where your code's slow and how you should optimize it.

Talk Python's Mastodon Michael Kennedy's Mastodon