3

I saw this default values usage in the Python's Queue module:

def _put(self, item, heappush=heapq.heappush):
    heappush(self.queue, item)

def _get(self, heappop=heapq.heappop):
    return heappop(self.queue)

I wonder why the variables are used as function arguments here? Is it just a matter of taste or some kind of optimization?

0

1 Answer 1

4

It's a micro optimization. Default values are evaluated only once at function definition time, and locals (including parameters) are a bit faster to access than globals, they're implemented as a C array lookup instead of a dict lookup. It also allows avoiding repeatedly looking up the heappush and heappop members of heapq, without polluting the namespace by pulling them in directly.

Timeit snippets:

python -mtimeit --setup "import heapq" --setup "def f(q,x,p=heapq.heappush): p(q,x)" "f([], 1)"
1000000 loops, best of 3: 0.538 usec per loop

python -mtimeit --setup "import heapq" --setup "def f(q,p=heapq.heappop): p(q)" "f([1])"
1000000 loops, best of 3: 0.386 usec per loop

python -mtimeit --setup "import heapq" --setup "def f(q,x): heapq.heappush(q,x)" "f([], 1)"
1000000 loops, best of 3: 0.631 usec per loop

python -mtimeit --setup "import heapq" --setup "def f(q): heapq.heappop(q)" "f([1])"
1000000 loops, best of 3: 0.52 usec per loop
Sign up to request clarification or add additional context in comments.

3 Comments

Can you give a reference for the array lookup?
+1, it saves a few nanoseconds in Py2.7 on my box. Note that the current tip does import the heap functions into its namespace: hg.python.org/cpython/file/tip/Lib/queue.py
@ramcdougal For being faster: 1 For being an array lookup: First, dis shows that local and global names use different bytecode operations (LOAD_GLOBAL vs LOAD_FAST). Second, digging through ceval.c will show that LOAD_FAST is mapped to GETLOCAL which is a macro defined earlier in the same file which expands to fastlocals[i] which is a dynamically allocated array in the various functions in ceval.c that perform interpretation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.