Hacker News new | ask | show | jobs
by mrwilliamchang 4274 days ago
Love the title.

A more common mistake I notice people making is writing code that makes more memory allocations than necessary.

  # Bad: Makes an extra instantiation of a list with 1 in
  # it which then needs to be read 
  x = set([1])

  # Good
  x = set()
  x.add(1)

Overall, I think it is important to remember that when you write a program that every step translates to a set of operations. And this applies to all kinds of programming, not just functional programming.
1 comments

Any half-decent optimizing compiler will turn those two implementations into the exact same assembly.

That being said, that looks like Python source code - and as the default Python interpreter doesn't do much in the way of optimization, you are correct in that case. (Although I wonder about PyPy.)