Hacker News new | past | comments | ask | show | jobs | submit login

I was reading some of the Python standard library code and one piece stood out for me in the heapq.py module.

Merge sorted iterables using min heap:

  def merge(*iterables):
      '''Merge multiple sorted inputs into a single sorted output.
      >>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25]))
      [0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25]
      '''
      _heappop, _heapreplace, _StopIteration = heappop, heapreplace, StopIteration
      _len = len

      h = []
      h_append = h.append
      for itnum, it in enumerate(map(iter, iterables)):
          try:
              next = it.next
              h_append([next(), itnum, next])
          except _StopIteration:
              pass
      heapify(h)

      while _len(h) > 1:
          try:
              while 1:
                  v, itnum, next = s = h[0]
                  yield v
                  s[0] = next()               # raises StopIteration when exhausted
                  _heapreplace(h, s)          # restore heap condition
          except _StopIteration:
              _heappop(h)                     # remove empty iterator
      if h:
          # fast case when only a single iterator remains
          v, itnum, next = h[0]
          yield v
          for v in next.__self__:
              yield v
If I'm not wrong, this is written by Raymond Hettinger and it's always a pleasure to read/use his code.



The scope lifting (I forget what this performance hack is called), `_len = len` etc., is unsatisfying.


I noticed that, but haven't any idea what the purpose is?


It's a speed optimization. Local lookups are faster than global lookups in Python.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: