Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/hgweb/webcommands.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 | 1aee2ab0f902 |
children | 41957e50e109 |
comparison
equal
deleted
inserted
replaced
26845:7a77ee434179 | 26846:7c1b4840c2cd |
---|---|
1246 node=ctx.hex(), changenav=changenav) | 1246 node=ctx.hex(), changenav=changenav) |
1247 | 1247 |
1248 def _getdoc(e): | 1248 def _getdoc(e): |
1249 doc = e[0].__doc__ | 1249 doc = e[0].__doc__ |
1250 if doc: | 1250 if doc: |
1251 doc = _(doc).split('\n')[0] | 1251 doc = _(doc).partition('\n')[0] |
1252 else: | 1252 else: |
1253 doc = _('(no help text available)') | 1253 doc = _('(no help text available)') |
1254 return doc | 1254 return doc |
1255 | 1255 |
1256 @webcommand('help') | 1256 @webcommand('help') |
1276 def topics(**map): | 1276 def topics(**map): |
1277 for entries, summary, _doc in helpmod.helptable: | 1277 for entries, summary, _doc in helpmod.helptable: |
1278 yield {'topic': entries[0], 'summary': summary} | 1278 yield {'topic': entries[0], 'summary': summary} |
1279 | 1279 |
1280 early, other = [], [] | 1280 early, other = [], [] |
1281 primary = lambda s: s.split('|')[0] | 1281 primary = lambda s: s.partition('|')[0] |
1282 for c, e in commands.table.iteritems(): | 1282 for c, e in commands.table.iteritems(): |
1283 doc = _getdoc(e) | 1283 doc = _getdoc(e) |
1284 if 'DEPRECATED' in doc or c.startswith('debug'): | 1284 if 'DEPRECATED' in doc or c.startswith('debug'): |
1285 continue | 1285 continue |
1286 cmd = primary(c) | 1286 cmd = primary(c) |