inspect.stack() performance

Doron Tal doron.tal.list at gmail.com
Thu Aug 6 14:00:45 CEST 2009


I use inspect.stack() to extract some info, such as file name, function name
and line number, for the sake of logging (home brew logger).
I tested to see how much time it takes to execute the command:

Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> t = timeit.Timer('inspect.stack()', 'import inspect')
>>> t.repeat(3,1000)
[0.28500604629516602, 0.28315305709838867, 0.29169297218322754]
>>>

About 280uSec per call.

If I log 1000 times per seconds than I get to spend 28% of the process time
on inspect.stack()
Is there a better/quicker way of retrieving the same info?

Thanks,
--doron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090806/ea21537d/attachment.html>


More information about the Python-list mailing list