473,223 Members | 1,727 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,223 software developers and data experts.

Bizarre additional calling overhead.

>>def test_func():
.... pass
....
>>import new
test_func2 = new.function(test_func.func_code, {}, "test_func2")
test_func2
<function test_func2 at 0x01B8C2F0>
>>test_func
<function test_func at 0x01B8C270>
>>import timeit
tf = timeit.Timer("test_func()", "from __main__ import test_func")
tf.repeat()
[0.2183461704377247, 0.18068215314489791, 0.17978585841498085]
>>tf2 = timeit.Timer("test_func2()", "from __main__ import test_func2")
tf2.repeat()
[0.40015390239890891, 0.35893452879396648, 0.36034628133737456]
>>>
Why almost twice the calling overhead for a dynamic function?
Nov 2 '07 #1
2 1096
On Nov 2, 3:08 pm, "Chris Mellon" <arka...@gmail.comwrote:
>def test_func():

... pass
...>>import new
>test_func2 = new.function(test_func.func_code, {}, "test_func2")
test_func2

<function test_func2 at 0x01B8C2F0>>>test_func

<function test_func at 0x01B8C270>>>import timeit
>tf = timeit.Timer("test_func()", "from __main__ import test_func")
tf.repeat()

[0.2183461704377247, 0.18068215314489791, 0.17978585841498085]>>tf2 = timeit.Timer("test_func2()", "from __main__ import test_func2")
>tf2.repeat()

[0.40015390239890891, 0.35893452879396648, 0.36034628133737456]

Why almost twice the calling overhead for a dynamic function?
So, I don't have an official explanation for why it takes twice as
long, but the only difference between the two functions I could find
was that test_func.func_globals was set to globals() and
test_func2.func_globals was an empty dict. When I re-created
test_func2 with globals set to globals() it ran just as fast as
test_func.
>>test_func2 = new.function(test_func.func_code, globals(), "test_func")
tf2 = timeit.Timer("test_func2()", "from __main__ import test_func2")
tf2.repeat()
[0.18119118491313202, 0.18396220748718406, 0.18722407456812107]
>>tf.repeat()
[0.18125124841299112, 0.17981251807145782, 0.18517996002287873]

Matt

Nov 3 '07 #2
On Fri, 02 Nov 2007 17:08:06 -0500, Chris Mellon wrote:
>>>def test_func():
... pass
...
>>>import new
test_func2 = new.function(test_func.func_code, {}, "test_func2")
[snip results of timeit]
Why almost twice the calling overhead for a dynamic function?
When I time the functions, I get a lot of variability. The minimum value
is probably the best one to look at:
>>min(timeit.Timer("test_func()",
.... "from __main__ import test_func").repeat(20))
0.35664010047912598
>>min(timeit.Timer("test_func2()",
.... "from __main__ import test_func2").repeat(20))
0.68138217926025391
Disassembling the code does not give any hints:
>>import dis
dis.dis(test_func)
2 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
>>dis.dis(test_func2)
2 0 LOAD_CONST 0 (None)
3 RETURN_VALUE

Which is what we should expect, because both functions have the same code:
>>test_func.func_code is test_func2.func_code
True
But try this:
>>test_func3 = new.function(test_func.func_code,
.... globals(), 'test_func3')
>>min(timeit.Timer("test_func3()",
.... "from __main__ import test_func3").repeat(20))
0.35772204399108887
Hmmm... it looks like the difference in speed has to do with the globals,
not the fact that it is created dynamically. I wonder why?


--
Steven.
Nov 3 '07 #3

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

Similar topics

4
by: Alan Little | last post by:
This is very bizarre. Could someone else have a look at this? Maybe you can see something I'm overlooking. Go here: http://www.newsletters.forbes.com/enews/admin/deliver.php4 U: bugtest P:...
1
by: Lakshmi | last post by:
Hi All, I am having performance issues with the .NET client calling the Java Webservice running on axis. Have detailed the problem below. Please help. I wrote a webservice in Java. Lets name...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
4
by: dave m | last post by:
I'm creating a class library (dll) and am confused as how to pass any information, should any exceptions occur, back to the calling application. Such as: Try x = 3 / 0 ' divide by 0 error...
6
by: Anthony Smith | last post by:
I can call a class using "->", but it complains about the :: I see on the net where :: is used. Is there a good explanation on when to use one over the other or the differences? $help = new...
3
by: Beorne | last post by:
I have a propertary library dll (used to drive a device) that I call from my C# code. Calling the functions from C++ is really faster than calling them in C+ +. From C++ the call is almost...
35
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with...
2
by: Tom | last post by:
My older system: Win2k, VS2005(Academic), .Net 2.0 SP1. Windows.Forms Application: Two splitter panels, a TreeView (named: "tree") in one panel populated with directory name nodes. Logic for...
20
by: Jasper | last post by:
I'm stumped. I'm calling a method that has keyword args, but not setting them, and yet one of them starts off with data?! The class definition begins like so: class BattleIntentionAction(...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.