Why are List Comprehensions Faster than Loops? [Python Disassembly]

Поделиться
HTML-код
  • Опубликовано: 28 сен 2024

Комментарии • 16

  • @sanderbos4243
    @sanderbos4243 Год назад +9

    Super clean explanation of not only the video's title, but also lots of useful tidbits like the stack and bytecode! :D

    • @DanielBoctor
      @DanielBoctor  Год назад +2

      Thank you! I'm glad you enjoyed 😊

  • @tiwongemhango955
    @tiwongemhango955 Год назад +5

    Thanks for this. This was quite helpful. Hope this channel grows. Subscribed and will keep watching 👍🏾

    • @DanielBoctor
      @DanielBoctor  Год назад +2

      I'm glad you liked it! I appreciate the words

  • @johannes8144
    @johannes8144 Год назад +7

    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 :)

    • @woojason7119
      @woojason7119 Год назад +3

      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

    • @forrest2851
      @forrest2851 Год назад +2

      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.

    • @DanielBoctor
      @DanielBoctor  Год назад +4

      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!!

    • @johannes8144
      @johannes8144 Год назад +2

      @@DanielBoctor thank you very much for your detailed response, quite a few things I did not know. You gained a follower :)

    • @DanielBoctor
      @DanielBoctor  Год назад +3

      Glad to have you aboard!

  • @woojason7119
    @woojason7119 Год назад +2

    Love explanations on python low level interpreting, thanks for it!

    • @DanielBoctor
      @DanielBoctor  Год назад +1

      I'm glad you liked it! Thanks for watching 😊

  • @manifestasisanubari
    @manifestasisanubari Год назад +2

    How about ternary operator?

  • @antonhelsgaun
    @antonhelsgaun Год назад +1

    Amazing video for the amount of views

  • @mystifoxtech
    @mystifoxtech 10 месяцев назад

    A simple solution to the problem of bad performance is to not use Python.

  • @TheControlMastr
    @TheControlMastr Год назад +2

    PhD in python loading… 🪫🔋