[Python-Dev] __file__ is not always an absolute path

Barry Warsaw barry at python.org
Sun Feb 7 19:54:16 CET 2010


On Feb 06, 2010, at 11:22 PM, exarkun at twistedmatrix.com wrote:

>>I haven't tried to repro this particular example, but the reason is
>>that we don't want to have to call getpwd() on every import nor do we
>>want to have some kind of in-process variable to cache the current
>>directory. (getpwd() is relatively slow and can sometimes fail
>>outright, and trying to cache it has a certain risk of being wrong.)
>
>Assuming you mean os.getcwd():
>
>exarkun at boson:~$ python -m timeit -s 'def f(): pass' 'f()'
>10000000 loops, best of 3: 0.132 usec per loop
>exarkun at boson:~$ python -m timeit -s 'from os import getcwd' 'getcwd()'
>1000000 loops, best of 3: 1.02 usec per loop
>exarkun at boson:~$
>So it's about 7x more expensive than a no-op function call.  I'd call 
>this pretty quick.  Compared to everything else that happens during an 
>import, I'm not convinced this wouldn't be lost in the noise.  I think 
>it's at least worth implementing and measuring.

I'd like to see the effect on command line scripts that are run often and then
exit, e.g. Bazaar or Mercurial.  Start up time due to import overhead seems to
be a constant battle for those types of projects.

-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-dev/attachments/20100207/572a4045/attachment.pgp>


More information about the Python-Dev mailing list