Mercurial > public > mercurial-scm > hg
comparison mercurial/hgweb/hgweb_mod.py @ 26846:7c1b4840c2cd
hgweb: replace some str.split() calls by str.partition() or str.rpartition()
Since Python 2.5 str has new methods: partition and rpartition. They are more
specialized than the usual split and rsplit, and they sometimes convey the
intent of code better and also are a bit faster (faster than split/rsplit with
maxsplit specified). Let's use them in appropriate places for a small speedup.
Example performance (partition):
$ python -m timeit 'assert "apple|orange|banana".split("|")[0] == "apple"'
1000000 loops, best of 3: 0.376 usec per loop
$ python -m timeit 'assert "apple|orange|banana".split("|", 1)[0] == "apple"'
1000000 loops, best of 3: 0.327 usec per loop
$ python -m timeit 'assert "apple|orange|banana".partition("|")[0] == "apple"'
1000000 loops, best of 3: 0.214 usec per loop
Example performance (rpartition):
$ python -m timeit 'assert "apple|orange|banana".rsplit("|")[-1] == "banana"'
1000000 loops, best of 3: 0.372 usec per loop
$ python -m timeit 'assert "apple|orange|banana".rsplit("|", 1)[-1] == "banana"'
1000000 loops, best of 3: 0.332 usec per loop
$ python -m timeit 'assert "apple|orange|banana".rpartition("|")[-1] == "banana"'
1000000 loops, best of 3: 0.219 usec per loop
author | Anton Shestakov <av6@dwimlabs.net> |
---|---|
date | Mon, 02 Nov 2015 23:37:49 +0800 |
parents | 4b0fc75f9403 |
children | c8cbef073645 |
comparison
equal
deleted
inserted
replaced
26845:7a77ee434179 | 26846:7c1b4840c2cd |
---|---|
302 repo_parts = req.env.get('REPO_NAME', '').split('/') | 302 repo_parts = req.env.get('REPO_NAME', '').split('/') |
303 if parts[:len(repo_parts)] == repo_parts: | 303 if parts[:len(repo_parts)] == repo_parts: |
304 parts = parts[len(repo_parts):] | 304 parts = parts[len(repo_parts):] |
305 query = '/'.join(parts) | 305 query = '/'.join(parts) |
306 else: | 306 else: |
307 query = req.env['QUERY_STRING'].split('&', 1)[0] | 307 query = req.env['QUERY_STRING'].partition('&')[0] |
308 query = query.split(';', 1)[0] | 308 query = query.partition(';')[0] |
309 | 309 |
310 # process this if it's a protocol request | 310 # process this if it's a protocol request |
311 # protocol bits don't need to create any URLs | 311 # protocol bits don't need to create any URLs |
312 # and the clients always use the old URL structure | 312 # and the clients always use the old URL structure |
313 | 313 |