For the list append, is it because of the use of len()? Just checked the code because I wanted to know why this is happening. Couldn't figure out the dictionary though.
Both are actually the same effect. When you call data.get(...), you're calling the method, of course, but also looking up the method in the data object's internal dictionary, called __dict__. What the above article is showing is that there's some savings to be made if you cache the lookup.
He's not optimizing a list at all, but he is optimizing a dict - in both cases.
Because data[num] = ... is actually data.set(num, ...), there's another candidate for optimization there, as well - but by now you can probably guess what it is.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Wow thanks! I had no idea this was even possible.
For the list
append
, is it because of the use oflen()
? Just checked the code because I wanted to know why this is happening. Couldn't figure out the dictionary though.Both are actually the same effect. When you call
data.get(...)
, you're calling the method, of course, but also looking up the method in thedata
object's internal dictionary, called__dict__
. What the above article is showing is that there's some savings to be made if you cache the lookup.He's not optimizing a list at all, but he is optimizing a dict - in both cases.
Because
data[num] = ...
is actuallydata.set(num, ...)
, there's another candidate for optimization there, as well - but by now you can probably guess what it is.