Python does not do a good job of appending strings: it takes O(n**2) time to append n strings.
Suggested-by: Reid Price <[email protected]> --- python/ovs/db/data.py | 58 +++++++++++++++++++++++++----------------------- 1 files changed, 30 insertions(+), 28 deletions(-) diff --git a/python/ovs/db/data.py b/python/ovs/db/data.py index a5fcb76..27f6f11 100644 --- a/python/ovs/db/data.py +++ b/python/ovs/db/data.py @@ -1,4 +1,4 @@ -# Copyright (c) 2009, 2010 Nicira Networks +# Copyright (c) 2009, 2010, 2011 Nicira Networks # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -35,30 +35,30 @@ class ConstraintViolation(error.Error): error.Error.__init__(self, msg, json, tag="constraint violation") def escapeCString(src): - dst = "" + dst = [] for c in src: if c in "\\\"": - dst += "\\" + c + dst.append("\\" + c) elif ord(c) < 32: if c == '\n': - dst += '\\n' + dst.append('\\n') elif c == '\r': - dst += '\\r' + dst.append('\\r') elif c == '\a': - dst += '\\a' + dst.append('\\a') elif c == '\b': - dst += '\\b' + dst.append('\\b') elif c == '\f': - dst += '\\f' + dst.append('\\f') elif c == '\t': - dst += '\\t' + dst.append('\\t') elif c == '\v': - dst += '\\v' + dst.append('\\v') else: - dst += '\\%03o' % ord(c) + dst.append('\\%03o' % ord(c)) else: - dst += c - return dst + dst.append(c) + return ''.join(dst) def returnUnchanged(x): return x @@ -335,31 +335,33 @@ class Datum(object): for k, v in sorted(self.values.items())]] def to_string(self): + head = tail = None if self.type.n_max > 1 or len(self.values) == 0: if self.type.is_map(): - s = "{" + head = "{" + tail = "}" else: - s = "[" - else: - s = "" + head = "[" + tail = "]" + + s = [] + if head: + s.append(head) i = 0 for key in sorted(self.values): - if i > 0: - s += ", " + if i: + s.append(", ") i += 1 + s.append(key.to_string()) if self.type.is_map(): - s += "%s=%s" % (key.to_string(), self.values[key].to_string()) - else: - s += key.to_string() + s.append("=") + s.append(self.values[key].to_string()) - if self.type.n_max > 1 or len(self.values) == 0: - if self.type.is_map(): - s += "}" - else: - s += "]" - return s + if tail: + s.append(tail) + return ''.join(s) def as_list(self): if self.type.is_map(): -- 1.7.4.4 _______________________________________________ dev mailing list [email protected] http://openvswitch.org/mailman/listinfo/dev
