Python Forum
optimization of identical lookup
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
optimization of identical lookup
#1
if i have code like:

import os
...
if os.path.lexists((mypath):
    if os.path.isdir(mypath):
...
does the compiler in CPython optimize this any, in particular not looking up "path" in os twice but saving it because it can see a future use just ahead?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I cannot guarantee that it is not optimized, but I'm almost certain of it. Furthermore, another process could probably remove the directory between the call to exists() and the call to isdir().
Reply
#3
so, in some cases, it might be a tad bit faster if i extract a reference to os.path and use that as in code using that variable name instead of os.path like pathvar.lexists().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020