aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Fernandes <joelaf@google.com>2017-06-25 13:40:26 -0700
committerJoel Fernandes <joelaf@google.com>2017-06-25 13:40:26 -0700
commitc0a49a16d85ef5b732fafe02faad73c67df7b665 (patch)
treedc35db4d0b67abdffed2b89511b6af1667959176
parentc9243e261fb37be1b1149ae6d111e18745b75959 (diff)
downloadtrappy-c0a49a16d85ef5b732fafe02faad73c67df7b665.tar.gz
trappy: Optimize integer conversion in generate_data_dict
generate_data_dict uses try/except for integer conversion. This is expensive consider the frequency of the calls. Optimize it by using a slightly more uglier but much fast version of the same. With this I get a speed of ~8.5% in parsing. Change-Id: I909ad170756fd284c7d924950945e755880ceb5f Signed-off-by: Joel Fernandes <joelaf@google.com>
-rw-r--r--trappy/base.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/trappy/base.py b/trappy/base.py
index 4502c77..6a8c822 100644
--- a/trappy/base.py
+++ b/trappy/base.py
@@ -179,6 +179,12 @@ class Base(object):
self.line_array.append(line)
self.data_array.append(data)
+ def conv_to_int(self, value):
+ # Handle false-positives for negative numbers
+ if value.lstrip("-").isdigit():
+ value = int(value)
+ return value
+
def generate_data_dict(self, data_str):
data_dict = {}
prev_key = None
@@ -190,10 +196,7 @@ class Base(object):
data_dict[prev_key] += ' ' + field
continue
(key, value) = field.split('=', 1)
- try:
- value = int(value)
- except ValueError:
- pass
+ value = self.conv_to_int(value)
data_dict[key] = value
prev_key = key
return data_dict