Menu

#2 repr(?) is very slow

open
rimcaw
None
5
2012-09-14
2008-04-12
No

import avl

t = avl.new()
for i in range(2**16):
t.insert(i)

t

takes more than 10 seconds on a 2.4GHz machine

list(t)

'instant'

I think the problem is the infamous O(n^2) array concat problem with strings. The repr function works by concating each item's repr onto a string.

I don't have a patch, but there are two obvious solutions: a) convert to a list and return repr for that b) construct the string in bottom up order from the tree - concat reprs from neighbouring branches into a single string and move up the tree:

repr(node):
return repr(node.left) + "," + repr(node.right)

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.