473,381 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

Speed of Nested Functions & Lambda Expressions

Hi All,

It is really convenient to use nested functions and lambda
expressions. What I'd like to know is if Python compiles fn_inner()
only once and change the binding of v every time fn_outer() is called
or if Python compile and generate a new function object every time. If
it is the latter, will there be a huge performance hit? Would someone
give some hint about how exactly Python does this internally?

def fn_outer(v):
a=v*2
def fn_inner():
print "V:%d,%d" % (v,a)

fn_inner()

Thanks,
Geoffrey

Oct 23 '07 #1
5 4416
beginner wrote:
Hi All,

It is really convenient to use nested functions and lambda
expressions. What I'd like to know is if Python compiles fn_inner()
only once and change the binding of v every time fn_outer() is called
or if Python compile and generate a new function object every time. If
it is the latter, will there be a huge performance hit? Would someone
give some hint about how exactly Python does this internally?

def fn_outer(v):
a=v*2
def fn_inner():
print "V:%d,%d" % (v,a)

fn_inner()

Thanks,
Geoffrey

The code is compiled only once when the file is initially read in.
During execution of fn_outer, v will be bound to a value, then a, then
fn_inner will be bound (to an already compiled code object) and so on.

Really, from the point of view of Python while executing fn_outer, the
def of fn_inner looks just like an assignment with fn_inner as the
variable name and a code object as the value.

Gary Herron
Oct 23 '07 #2
On Oct 23, 11:06 am, Gary Herron <gher...@islandtraining.comwrote:
beginner wrote:
Hi All,
It is really convenient to use nested functions and lambda
expressions. What I'd like to know is if Python compiles fn_inner()
only once and change the binding of v every time fn_outer() is called
or if Python compile and generate a new function object every time. If
it is the latter, will there be a huge performance hit? Would someone
give some hint about how exactly Python does this internally?
def fn_outer(v):
a=v*2
def fn_inner():
print "V:%d,%d" % (v,a)
fn_inner()
Thanks,
Geoffrey

The code is compiled only once when the file is initially read in.
During execution of fn_outer, v will be bound to a value, then a, then
fn_inner will be bound (to an already compiled code object) and so on.

Really, from the point of view of Python while executing fn_outer, the
def of fn_inner looks just like an assignment with fn_inner as the
variable name and a code object as the value.

Gary Herron- Hide quoted text -

- Show quoted text -
I see. Thanks Gary!

Oct 23 '07 #3

"Gary Herron" <gh*****@islandtraining.comwrote in message
news:47**************@islandtraining.com...
| beginner wrote:
| Hi All,
| >
| It is really convenient to use nested functions and lambda
| expressions. What I'd like to know is if Python compiles fn_inner()
| only once and change the binding of v every time fn_outer() is called
| or if Python compile and generate a new function object every time. If
| it is the latter, will there be a huge performance hit? Would someone
| give some hint about how exactly Python does this internally?

Python the language does not 'do' anything. The details could be
implementation specific.

| def fn_outer(v):
| a=v*2
| def fn_inner():
| print "V:%d,%d" % (v,a)
| >
| fn_inner()
| >
| Thanks,
| Geoffrey
| >
| >
| The code is compiled only once when the file is initially read in.
| During execution of fn_outer, v will be bound to a value, then a, then
| fn_inner will be bound (to an already compiled code object) and so on.

In CPython, I believe, fn_inner is bound to a *new* *function* object, not
the once-compiled code object. It has to be a new object because fn_outer
could return the inner function, as is not uncommon. And function objects
are somewhat mutable. And each returned function might have different
values of enclosed outer variables. However, each function object has the
same .func_code member.
>>def outer(n):
def inner(m):
return n+m
return inner
>>add3 = outer(3)
add3(4)
7
>>type(add3)
<type 'function'>

| Really, from the point of view of Python while executing fn_outer, the
| def of fn_inner looks just like an assignment with fn_inner as the
| variable name and a code object as the value.

Again, the value is a function object.

Terry Jan Reedy

Oct 24 '07 #4
beginner <zy*******@gmail.comwrote:
It is really convenient to use nested functions and lambda
expressions. What I'd like to know is if Python compiles fn_inner()
only once and change the binding of v every time fn_outer() is called
or if Python compile and generate a new function object every time. If
it is the latter, will there be a huge performance hit? Would someone
give some hint about how exactly Python does this internally?
You can use Python's bytecode disassembler to see what actually gets
executed here:
>>def fn_outer(v):
a=v*2
def fn_inner():
print "V:%d,%d" % (v,a)

fn_inner()

>>import dis
dis.dis(fn_outer)
2 0 LOAD_DEREF 1 (v)
3 LOAD_CONST 1 (2)
6 BINARY_MULTIPLY
7 STORE_DEREF 0 (a)

3 10 LOAD_CLOSURE 0 (a)
13 LOAD_CLOSURE 1 (v)
16 BUILD_TUPLE 2
19 LOAD_CONST 2 (<code object fn_inner at
01177218, file "<pyshell#3>", line 3>)
22 MAKE_CLOSURE 0
25 STORE_FAST 1 (fn_inner)

6 28 LOAD_FAST 1 (fn_inner)
31 CALL_FUNCTION 0
34 POP_TOP
35 LOAD_CONST 0 (None)
38 RETURN_VALUE
>>>
When you execute the 'def' statement, the two scoped variables a and v
are built into a tuple on the stack, the compiled code object for the
inner function is also pushed onto the stack and then the function is
created by the 'MAKE_CLOSURE' instruction. This is then stored in a
local variable (STORE_FAST) which is then loaded and called.

So the function definition is pretty fast, BUT notice how fn_inner is
referenced by STORE_FAST/LOAD_FAST whereas a and v are referenced by
LOAD_DEREF/STORE_DEREF and LOAD_CLOSURE.

The code for fn_inner also uses LOAD_DEREF to get at the scoped
variables:

4 0 LOAD_CONST 1 ('V:%d,%d')
3 LOAD_DEREF 1 (v)
6 LOAD_DEREF 0 (a)
9 BUILD_TUPLE 2
12 BINARY_MODULO
13 PRINT_ITEM
14 PRINT_NEWLINE
15 LOAD_CONST 0 (None)
18 RETURN_VALUE

(its a bit harder to disassemble that one, I stuck a call to dis.dis
inside fn_outer to get that)

If you do some timings you'll find that LOAD_DEREF/STORE_DEREF are
rather slower than LOAD_FAST/STORE_FAST, so while the overhead for
creating the function is minimal you could find that if you access the
variables a lot (even in fn_outer) there may be a measurable slow-down.

If timings show that it is a code hotspot then you might find it better
to nest the function but pass any required values in as parameters (but
if you don't have evidence for this just write whatever is clearest).
Oct 24 '07 #5
On Oct 24, 2:52 am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
beginner <zyzhu2...@gmail.comwrote:
It is really convenient to use nested functions and lambda
expressions. What I'd like to know is if Python compiles fn_inner()
only once and change the binding of v every time fn_outer() is called
or if Python compile and generate a new function object every time. If
it is the latter, will there be a huge performance hit? Would someone
give some hint about how exactly Python does this internally?

You can use Python's bytecode disassembler to see what actually gets
executed here:
>def fn_outer(v):

a=v*2
def fn_inner():
print "V:%d,%d" % (v,a)

fn_inner()
>import dis
dis.dis(fn_outer)

2 0 LOAD_DEREF 1 (v)
3 LOAD_CONST 1 (2)
6 BINARY_MULTIPLY
7 STORE_DEREF 0 (a)

3 10 LOAD_CLOSURE 0 (a)
13 LOAD_CLOSURE 1 (v)
16 BUILD_TUPLE 2
19 LOAD_CONST 2 (<code object fn_inner at
01177218, file "<pyshell#3>", line 3>)
22 MAKE_CLOSURE 0
25 STORE_FAST 1 (fn_inner)

6 28 LOAD_FAST 1 (fn_inner)
31 CALL_FUNCTION 0
34 POP_TOP
35 LOAD_CONST 0 (None)
38 RETURN_VALUE

When you execute the 'def' statement, the two scoped variables a and v
are built into a tuple on the stack, the compiled code object for the
inner function is also pushed onto the stack and then the function is
created by the 'MAKE_CLOSURE' instruction. This is then stored in a
local variable (STORE_FAST) which is then loaded and called.

So the function definition is pretty fast, BUT notice how fn_inner is
referenced by STORE_FAST/LOAD_FAST whereas a and v are referenced by
LOAD_DEREF/STORE_DEREF and LOAD_CLOSURE.

The code for fn_inner also uses LOAD_DEREF to get at the scoped
variables:

4 0 LOAD_CONST 1 ('V:%d,%d')
3 LOAD_DEREF 1 (v)
6 LOAD_DEREF 0 (a)
9 BUILD_TUPLE 2
12 BINARY_MODULO
13 PRINT_ITEM
14 PRINT_NEWLINE
15 LOAD_CONST 0 (None)
18 RETURN_VALUE

(its a bit harder to disassemble that one, I stuck a call to dis.dis
inside fn_outer to get that)

If you do some timings you'll find that LOAD_DEREF/STORE_DEREF are
rather slower than LOAD_FAST/STORE_FAST, so while the overhead for
creating the function is minimal you could find that if you access the
variables a lot (even in fn_outer) there may be a measurable slow-down.

If timings show that it is a code hotspot then you might find it better
to nest the function but pass any required values in as parameters (but
if you don't have evidence for this just write whatever is clearest).

Thanks for the detailed analysis, Duncan. Also thanks for showing how
the disassembler can be used to figure this out. I was just looking
for a tool like this. This is great. Thanks again.

Oct 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Sylvain Hellegouarch | last post by:
Hi, A bit off topic. I just wondered what was your feeling when you were coding with Python. I have beebn coding with different languages and the only that has given me the will to invent or...
53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
76
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax,...
37
by: Kay Schluehr | last post by:
Since George Sakkis proposed a new way of doing list comprehensions http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/ac5023ad18b2835f/d3ff1b81fa70c8a7#d3ff1b81fa70c8a7 ...
12
by: Kay Schluehr | last post by:
Hi all, thanks for Your attention ! I think my proposal was more in mind of Rons modified exec than Pythons lambda. When George proposed his unpacking behavoir for list-comps as a pack of...
12
by: Paul Miller | last post by:
I see lambda is "going away", so I want to use something that will be around for awhile. All I want to do is provide an "inline function" as an argument to another function. For example,...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
5
by: I V | last post by:
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote: With Ivan's approach, you lose access to the actual lambdas, so you need to create a new function and then modify its code object to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.