I really enjoyed your video. But there are still things i am missing here: 1. Map with lamda: list(map(lambda x: x*2, nums)) -> same performance or different performance 2. Bytecode of the map function? 3. Since you mostly only benchmarked your code with numbers: maybe an implementation via numpy would also been very nice. But still a very good video, and keep going :)
There’s a possibility the lambda could be faster - a named function must be looked up in the global namespace every time, an anonymous function… ? I don’t know. It shouldn’t need to be looked up. I have no idea, though.
Great questions @johannes8144! 1. Same performance. I should have included the lambda function in the video, however, it is essentially identical to a named function, because it will still need to be called as it's own frame, as @woojason7119 mentioned. Something important to note is that (possibly) contrary to what you might think, the lambda function will only be defined ONCE when used with map, not N number of times. This is because lambdas behave in the same way that regular functions do, in the sense that they can be passed around as objects in memory. What you are handing the map function when you give it a lamdba is the memory address of the lambda function. You can test this with: print(lambda x: x*2), as a memory address will be printed. Overall, there is no meaningful difference if you use map with a lambda. 2. You can go ahead and disassemble the map function, however you will not gain very much information from it. This is because the map function itself is implemented in CPython's built-in code, so the disassembler will only show you a CALL_FUNCTION and subsequent RETURN_VALUE for the map function. This makes sense if you think about it, because map is not written in Python itself. 3. This could be done blazing fast with numpy: import numpy as np nums = np.arange(10_000) doubles = nums * 2 Speed is in the order of ~10 µs, opposed to 350 µs. I also discussed NumPy more deeply in my Time Complexity video, incase you were interested. Thanks for watching!!
Super clean explanation of not only the video's title, but also lots of useful tidbits like the stack and bytecode! :D
Thank you! I'm glad you enjoyed 😊
Thanks for this. This was quite helpful. Hope this channel grows. Subscribed and will keep watching 👍🏾
I'm glad you liked it! I appreciate the words
I really enjoyed your video. But there are still things i am missing here:
1. Map with lamda: list(map(lambda x: x*2, nums)) -> same performance or different performance
2. Bytecode of the map function?
3. Since you mostly only benchmarked your code with numbers: maybe an implementation via numpy would also been very nice.
But still a very good video, and keep going :)
1. I guess it will be the same as calling a self-defined function, as lambda function will still put the function into call stack
There’s a possibility the lambda could be faster - a named function must be looked up in the global namespace every time, an anonymous function… ? I don’t know. It shouldn’t need to be looked up. I have no idea, though.
Great questions @johannes8144!
1. Same performance. I should have included the lambda function in the video, however, it is essentially identical to a named function, because it will still need to be called as it's own frame, as @woojason7119 mentioned. Something important to note is that (possibly) contrary to what you might think, the lambda function will only be defined ONCE when used with map, not N number of times. This is because lambdas behave in the same way that regular functions do, in the sense that they can be passed around as objects in memory. What you are handing the map function when you give it a lamdba is the memory address of the lambda function. You can test this with: print(lambda x: x*2), as a memory address will be printed.
Overall, there is no meaningful difference if you use map with a lambda.
2. You can go ahead and disassemble the map function, however you will not gain very much information from it. This is because the map function itself is implemented in CPython's built-in code, so the disassembler will only show you a CALL_FUNCTION and subsequent RETURN_VALUE for the map function. This makes sense if you think about it, because map is not written in Python itself.
3. This could be done blazing fast with numpy:
import numpy as np
nums = np.arange(10_000)
doubles = nums * 2
Speed is in the order of ~10 µs, opposed to 350 µs. I also discussed NumPy more deeply in my Time Complexity video, incase you were interested.
Thanks for watching!!
@@DanielBoctor thank you very much for your detailed response, quite a few things I did not know. You gained a follower :)
Glad to have you aboard!
Love explanations on python low level interpreting, thanks for it!
I'm glad you liked it! Thanks for watching 😊
How about ternary operator?
Amazing video for the amount of views
A simple solution to the problem of bad performance is to not use Python.
PhD in python loading… 🪫🔋