Fredrik Lundh wrote: > > the _flatten method in Tkinter is one of the major performance > bottlenecks for Python/Tk programmers. the method "flattens" > a nested sequence, by repeatedly adding tuples to each other. > > obviously, this isn't very efficient if the sequences are longer > than, say, five to ten items... and has of course led to claims > like "wxPython is a thousand times faster than Tkinter". > > anyway, taking recent changes to Python into account, it should > be possible to speed this up quite a bit. > > the old code looked like this: > > def _flatten(tuple): > res = () > for item in tuple: > if type(item) in (TupleType, ListType): > res = res + _flatten(item) > elif item is not None: > res = res + (item,) > return res > > after a some trials and errors, here's my proposed replacement: > > def _flatten1(seq): > res = [] > for item in seq: > if type(item) in (TupleType, ListType): > res.extend(_flatten1(item)) > elif item is not None: > res.append(item) > return res > > def _flatten(seq): > return tuple(_flatten1(seq)) > > in my tests, this is slighly faster on very short sequences (two > coordinate pairs, which is a common case for e.g. rectangles), > and much faster on long sequences (polylines, polygons). > > for example, for a 5000-point line, it's about 15 times faster. > on a 10,000 point line, it's over 50 times faster. etc. > > comments? Why not implement this in C and add it to _tkinter.c ? It might even make a nice standard builtin... -- Marc-Andre Lemburg ______________________________________________________________________ Business: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4