Hacker News new | past | comments | ask | show | jobs | submit | coady's comments login

Nice, a couple more recommendations:

* Use update instead of get then save. It's shorter, faster, and safe from race conditions.

* Use values{_list} instead of objects when only a few fields are required.

Those two anti-patterns are so prevalent I created a package dedicated to discouraging them: https://pypi.python.org/pypi/django-model-values.


Writing the first examples in a more idiomatic style would better demonstrate the timing differences. The list construction isn't really noticeable at that size.

  def generate(num):
      for i in xrange(num):
          yield random.randrange(10)

  def create_list(num):
      return [random.randrange(10) for i in xrange(num)]
Also worth noting that functional programming in Python (e.g. inlining loops) is often faster.

  from future_builtins import map
  import itertools

  def generate(num):
      return map(random.randrange, itertools.repeat(10, num))


Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: