improve filename_from_url() performance
Manually extracting the part between the last '/' and '?' instead of relying on the standard libraries' 'urllib.parse.urlsplit()' increases performance by ~400%. urlsplit() : 3.64 secs per 1.000.000 iterations partition(): 0.87 secs per 1.000.000 iterations
This commit is contained in:
@@ -70,7 +70,7 @@ def ensure_http_scheme(url, scheme="https://"):
|
|||||||
def filename_from_url(url):
|
def filename_from_url(url):
|
||||||
"""Extract the last part of an URL to use as a filename"""
|
"""Extract the last part of an URL to use as a filename"""
|
||||||
try:
|
try:
|
||||||
return urllib.parse.urlsplit(url).path.rpartition("/")[2]
|
return url.partition("?")[0].rpartition("/")[2]
|
||||||
except (TypeError, AttributeError):
|
except (TypeError, AttributeError):
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user