Issue 1560161: Better/faster implementation of os.path.split (original) (raw)
hi,
os.path.split is quite bad regarding performance on long pathnames:
def split(p): i = p.rfind('/') + 1 head, tail = p[:i], p[i:] if head and head != '/'*len(head): head = head.rstrip('/') return head, tail
especially this: '/'*len(head) this constructs an unnecessary string sometimes thousands of chars long.
better would be: if head and len(head) != head.count('/')
BUT: what is this 'if head and head != '/'*len(head):' for? this if is imho useless, because if head exists and is not all '/' => rstrip '/'
imho better would be: rstrip '/' from head and if head is empty add a '/' would be the same effect, because a singel '/' is just the same as a path as '/'*len(head).
def split(p): i = p.rfind('/') + 1 head, tail = p[:i], p[i:] head = head.rstrip('/') if not head: head = '/' return head, tail
such a implementation would be ways faster for long pathnames.
greets, michael