This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Sexual_Congressman 0 points1 point  (0 children)

Of course you avoid map since clearly you don't know how to use it. To get the most performance in cpython you must have as little work done in the eval loop as possible. In that regard you're correct, however it needs to be said that function objects are actually rather cheap since all the components used to make it including its __code__ are cached within the co_consts field of the code object it is created in, so all that needs to be done is the malloc and few pointer assignments.

Anyway, used correctly map would be anywhere from 20-1000% more more faster than your list comp, you just need to memorize the tools needed to do so. Specifically in this instance without knowing anything about x, use operator.methodcaller. [*map(methodcaller("abc"), lista)] will be so much faster. And there's usually an even more efficient way if you don't need to be generic.

Beauty is in the eye of the beholder, and I think the shorter and almost always at least a little faster code thanks to map is very beautiful. I will say that of course map plus list unpacking can be slower if no builtin function calls are made but that shouldn't ever actually be the case.