From dd27bc2111bbd415f7e392e1e32117cb903c5872 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 18 Apr 2012 18:39:38 +0200
Subject: [PATCH] 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: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/utils/algo.py | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/lib/utils/algo.py b/lib/utils/algo.py
index b1ffaf413..ec8ce348a 100644
--- a/lib/utils/algo.py
+++ b/lib/utils/algo.py
@@ -32,7 +32,6 @@ from ganeti.utils import text
 
 _SORTER_GROUPS = 8
 _SORTER_RE = re.compile("^%s(.*)$" % (_SORTER_GROUPS * "(\D+|\d+)?"))
-_SORTER_DIGIT = re.compile("^\d+$")
 
 
 def UniqueSequence(seq):
@@ -100,7 +99,7 @@ def _NiceSortTryInt(val):
   """Attempts to convert a string to an integer.
 
   """
-  if val and _SORTER_DIGIT.match(val):
+  if val and val.isdigit():
     return int(val)
   else:
     return val
-- 
GitLab