def upgrade(url):
"""Upgrade HTTP URL to HTTPS URL."""
components = urllib.parse.urlsplit(url)
if components.scheme.lower() == 'http':
return urllib.parse.urlunsplit(['https'] + list(components[1:5]))
else:
return url
def upgrade1(url):
"""Optimized version of upgrade, 10x faster."""
if url[:5].lower() == 'http:':
return 'https' + url[4:]
else:
return url
用timeit分别测试100万次,耗时分别如下:
4.574674844741821
0.46399807929992676
什么时候用库函数,什么时候用基本的函数实现,需要综合考虑开发速度、灵活性和性能等因素。
没有评论:
发表评论