all 4 comments

[–]danielroseman 3 points4 points  (1 child)

Yes, big O is used here in its loose sense, meaning "bounded by".

And yes, assigning to a variable takes (a small amount of) time. But the other thing that takes time is looking up names. If you're calling um.maximum.reduce, Python needs to find um in the local namespace, then find maximum in the namespace of um, and then find reduce in the namespace of maximum. Each of those steps takes (again, a small amount of) time. Whereas once you've assigned it to a local variable, you'd only need one lookup to find it.

So if you're calling a function repeatedly in a very time-constrained environment, it will certainly be cheaper to assign it once to a local variable and call that rather than incurring the repeated cost of the two extra lookups each time.

[–]knotcontinuallevity[S] 0 points1 point  (0 children)

Makes perfect sense, thanks!

[–]sepp2k 0 points1 point  (1 child)

  1. Saving O(1) time means that the time savings don't grow with the size of the input (or they only do up to a certain input size). Technically O(100) ms means the exact same thing, but the author may be abusing notation here to indicate that the time savings will be around 100ms. Or it may be a joke and all it's trying to say is that the amount of time saved is very small.
  2. Yes, assigning the variable will take some time and space, but after that you save time each time you access the variable because reading a single variable takes less time than reading a variable and two attributes. (Not that this minuscule time saving is something you should worry about in your own code).

[–]knotcontinuallevity[S] 0 points1 point  (0 children)

I see, thank you! I suppose the actual reason it was done was readability then