Bizarre additional calling overhead.

Matimus mccredie at gmail.com
Sat Nov 3 01:07:19 CET 2007


On Nov 2, 3:08 pm, "Chris Mellon" <arka... at gmail.com> wrote:
> >>> 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




More information about the Python-list mailing list