#100DaysOfCode in Python Transcripts
Chapter: Days 49-51: Measuring performance
Lecture: A quantum warning
0:00 Now, before I turn you loose
0:01 to go work on the code on your own
0:04 and do your own profiling,
0:06 I want to give you a short warning.
0:08 It's not a major warning,
0:09 but there is an effect you really need to be aware of.
0:13 There's some parallels here with quantum mechanics;
0:15 in quantum mechanics
0:16 we have Heisenberg's uncertainty principle
0:18 where it talks about the more precisely you know
0:21 the position of something, the less precisely
0:24 you can tell its speed and momentum.
0:26 So, by measuring one thing really closely
0:28 you have actually weakened your understanding of the other.
0:31 And profilers are like this as well.
0:33 You have your code operating one way
0:36 and you measure it really deeply,
0:38 actually put tons of hooks into it to do
0:40 this monitoring or porting with cProfile
0:44 and it might actually change the performance of your code.
0:46 So, when you look at those numbers,
0:48 like for example we said our program takes
0:50 610 milliseconds to run.
0:52 Maybe it only takes 400 milliseconds to run,
0:53 but with profiling it takes 600
0:55 and these effects are not evenly distributed.
0:59 It's not like, "Well, the whole program slows down 20%."
1:01 No. Some of it is barely affected.
1:03 Some of it's massively affected
1:06 and I would say a general rule is:
1:09 the more things you do in small pieces,
1:12 that's affected more.
1:14 The stuff that's kind of pushed into the standard library,
1:16 it doesn't have it's hooks in there.
1:18 So, for example that parse row loop
1:21 looping over every row in CSV
1:23 probably has some affect where as the sort,
1:27 that's one line measured
1:29 and then it's handed off to CPython
1:31 and then it's time when it's back.
1:32 So, there might be a ton of operations in there
1:34 but they're not measured.
1:36 So, just be aware this Heisenberg uncertainty principle
1:39 going on with profilers also somewhat applies to debuggers.
1:43 So, they kind of both apply here
1:45 but in this context we're concerned about
1:47 profilers and timing and it really can have a big affect.
1:50 That said, these profilers are massively helpful
1:54 and much better than your intuition
1:55 on understanding where your code's slow
1:57 and how you should optimize it.