Skip to content
Snippets Groups Projects
Commit dd27bc21 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

utils.algo: Use str.isdigit instead of regular expression


str.isdigit is about 4x faster than using a regular expression ("\d+").
This is in the inner sorting code so speed matters.

$ python -m timeit -s 'import re; s = re.compile("^\d+$")' \
's.match(""); s.match("Hello World"); s.match("1234")'
1000000 loops, best of 3: 0.937 usec per loop

$ python -m timeit '"".isdigit(); "Hello World".isdigit(); "1234".isdigit()'
1000000 loops, best of 3: 0.218 usec per loop

Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent b5800ee9
No related branches found
No related tags found
No related merge requests found
...@@ -32,7 +32,6 @@ from ganeti.utils import text ...@@ -32,7 +32,6 @@ from ganeti.utils import text
_SORTER_GROUPS = 8 _SORTER_GROUPS = 8
_SORTER_RE = re.compile("^%s(.*)$" % (_SORTER_GROUPS * "(\D+|\d+)?")) _SORTER_RE = re.compile("^%s(.*)$" % (_SORTER_GROUPS * "(\D+|\d+)?"))
_SORTER_DIGIT = re.compile("^\d+$")
def UniqueSequence(seq): def UniqueSequence(seq):
...@@ -100,7 +99,7 @@ def _NiceSortTryInt(val): ...@@ -100,7 +99,7 @@ def _NiceSortTryInt(val):
"""Attempts to convert a string to an integer. """Attempts to convert a string to an integer.
""" """
if val and _SORTER_DIGIT.match(val): if val and val.isdigit():
return int(val) return int(val)
else: else:
return val return val
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment