On 26 July 2014 05:03, Raoul Duke <[email protected]> wrote:
>> It works well in Python, and mostly well in .NET x32.
>
> ja wohl. but, what is the overhead? i'd love to be able to log like
crazy. :-)
>
> (ha ha "overhead" that's a joke in python oh boy i crack myself up.)
With CPython:
[wleslie@dirac:~/src/port-fairy]
17:24:47 $ cat frametest.py
import timeit
# control
print timeit.Timer(
"f()", """
import sys
def f():
result = []
frame = sys._getframe
for _ in xrange(4):
code = f.func_code
result.append((code.co_firstlineno, code.co_filename, code.co_name))
frame = 7
return result
""").timeit(100000)
print timeit.Timer(
"f()", """
import sys
def f():
result = []
frame = sys._getframe()
while frame is not None:
code = frame.f_code
result.append((frame.f_lineno, code.co_filename, code.co_name))
frame = frame.f_back
return result
""").timeit(100000)
[wleslie@dirac:~/src/port-fairy]
17:24:58 $ python frametest.py
0.167432069778
0.20387005806
For reference, result is this:
[(11, '<timeit-src>', 'f'), (17, '<timeit-src>', 'inner'), (195,
'/usr/lib/python2.7/timeit.py', 'timeit'), (15, 'frametest.py', '<module>')]
So I can access almost all of the information required for the traceback
about 500,000 times per second on my laptop, and perhaps 25% of that work
is walking the stack. Those convenient 'previous frame' references would
be a bit harder to determine if python didn't store that information on
every function call, so in a language made for performance you could
imagine f_back and f_code would be a little more expensive.
Turning the filename and line number into the text of a traceback (by
looking at the file, which is probably cached) is significantly slower it
seems, as traceback.extract_stack (from the traceback module) does.
I dare say I don't know what work goes into maintaining f_lineno. I
understand it can be synthesised from the program counter together with
code.co_firstlineno and code.co_lnotab.
Now one thing about python is that frame objects are mutable - you can use
them to set local variables and such. And because existing optimising
compilers don't do sufficient effect analysis - and there is no interface
for read-only frame access - they assume (last I checked) that if you're
grabbing the frame, you probably just want your program to go very, very
slow (;
--
William Leslie
Notice:
Likely much of this email is, by the nature of copyright, covered under
copyright law. You absolutely MAY reproduce any part of it in accordance
with the copyright law of the nation you are reading this in. Any attempt
to DENY YOU THOSE RIGHTS would be illegal without prior contractual
agreement.
_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev