473,397 Members | 2,033 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,397 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 1103
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.