This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Better/faster implementation of os.path.basename/dirname
Type: Stage:
Components: None Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: einsteinmg, georg.brandl, jedie, josiahcarlson
Priority: normal Keywords:

Created on 2006-09-17 14:55 by einsteinmg, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
posixpath.py_speedimprovments1.patch georg.brandl, 2006-09-18 12:08
Messages (5)
msg29861 - (view) Author: Michael Gebetsroither (einsteinmg) Date: 2006-09-17 14:55
hi,

basename/dirname could do better (especially on long 
pathnames)

def basename(p):
    return split(p)[1]

def dirname(p):
    return split(p)[0]

both construct base and dirname and discard the unused 
one.

what about that?

def basename(p):
    i = p.rfind('/') + 1
    return p[i:]

def dirname(p):
    i = p.rfind('/') + 1
    return p[:i]

greets,
michael
msg29862 - (view) Author: Michael Gebetsroither (einsteinmg) Date: 2006-09-18 12:42
Logged In: YES 
user_id=1600082

posixpath with this patch passes all test from 
test_posixpath cleanly.

benchmark:
basename( 310 ) means basename called with a 310 chars long 
path

sum = 0.0435626506805 min = 4.19616699219e-05 
posixpath.basename( 310 )
sum = 0.152147769928 min = 0.00014591217041 
posixpath_orig.basename( 310 )

sum = 0.0436658859253 min = 4.07695770264e-05 
posixpath.basename( 106 )
sum = 0.117312431335 min = 0.000112771987915 
posixpath_orig.basename( 106 )

sum = 0.0426909923553 min = 4.07695770264e-05 
posixpath.basename( 21 )
sum = 0.113305330276 min = 0.000110864639282 
posixpath_orig.basename( 21 )

sum = 0.12392115593 min = 0.000121831893921 
posixpath.dirname( 310 )
sum = 0.152860403061 min = 0.00014591217041 
posixpath_orig.dirname( 310 )

sum = 0.0942873954773 min = 9.08374786377e-05 
posixpath.dirname( 106 )
sum = 0.114937067032 min = 0.000111818313599 
posixpath_orig.dirname( 106 )

sum = 0.0918889045715 min = 8.79764556885e-05 
posixpath.dirname( 21 )
sum = 0.114675760269 min = 0.000109910964966 
posixpath_orig.dirname( 21 )

greets
msg29863 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-10-12 13:08
Logged In: YES 
user_id=849994

Committed in rev. 52316.
msg29864 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2006-10-16 22:23
Logged In: YES 
user_id=341410

I note that in the current SVN, dirname uses a test of "if
head and head != '/'*len(head):" to check for the path being
all /, could be replaced by "if head and head.count('/') !=
len(head):", but it probably isn't terribly important.
msg29865 - (view) Author: Jens Diemer (jedie) Date: 2007-01-23 06:21
A faster implementation is ok...

But why only posixpath patched? Why not ntpath and macpath updated, too?
History
Date User Action Args
2022-04-11 14:56:20adminsetgithub: 43987
2006-09-17 14:55:14einsteinmgcreate