473,289 Members | 1,840 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,289 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 1099
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.