diff options
author | Joel Fernandes <joelaf@google.com> | 2017-06-25 13:40:26 -0700 |
---|---|---|
committer | KP Singh <kpsingh@google.com> | 2017-06-29 23:11:15 +0200 |
commit | 1e326a177d920d27a4a5f0368095d875c6895c91 (patch) | |
tree | e5be639c4ed469164f617781a56d5c962488920c | |
parent | 9bb52fae4f0bb3f81da19cc2f0387260d7e21c0d (diff) | |
download | trappy-1e326a177d920d27a4a5f0368095d875c6895c91.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>
Reviewed-by: KP Singh <kpsingh@google.com>
-rw-r--r-- | trappy/base.py | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/trappy/base.py b/trappy/base.py index 4f2fb21..06857b5 100644 --- a/trappy/base.py +++ b/trappy/base.py @@ -180,6 +180,27 @@ class Base(object): self.line_array.append(line) self.data_array.append(data) + def string_cast(self, string, type): + """ Attempt to convert string to another type + + Here we attempt to cast string to a type. Currently only + integer conversion is supported with future expansion + left open to other types. + + :param string: The value to convert. + :type string: str + + :param type: The type to convert to. + :type type: type + """ + # Currently this function only supports int conversion + if type != int: + return + # Handle false-positives for negative numbers + if not string.lstrip("-").isdigit(): + return string + return int(string) + def generate_data_dict(self, data_str): data_dict = {} prev_key = None @@ -191,10 +212,7 @@ class Base(object): data_dict[prev_key] += ' ' + field continue (key, value) = field.split('=', 1) - try: - value = int(value) - except ValueError: - pass + value = self.string_cast(value, int) data_dict[key] = value prev_key = key return data_dict |