Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
sort order for strings of digits
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Steven D'Aprano, post: 5058494"] On the contrary. If you are using cmp with sort, your sorts are slow, and you should upgrade to using a key function as soon as possible. For small lists, you may not notice, but for large lists using a comparison function is a BAD IDEA. Here's an example: sorting a list of numbers by absolute value. py> L = [5, -6, 1, -2, 9, -8, 4, 3, -7, 2, -3] py> sorted(L, key=abs) [1, -2, 2, 3, -3, 4, 5, -6, -7, -8, 9] py> sorted(L, lambda a, b: cmp(abs(a), abs(b))) [1, -2, 2, 3, -3, 4, 5, -6, -7, -8, 9] But the amount of work done is radically different. Let's temporarily shadow the built-ins with patched versions: py> _abs = abs py> _abs, _cmp = abs, cmp py> c1 = c2 = 0 py> def abs(x): .... global c1 .... c1 += 1 .... return _abs(x) .... py> def cmp(a, b): .... global c2 .... c2 += 1 .... return _cmp(a, b) .... Now we can see just how much work is done under the hood using a key function vs a comparison function: py> sorted(L, key=abs) [1, -2, 2, 3, -3, 4, 5, -6, -7, -8, 9] py> c1 11 So the key function is called once for each item in the list. But: py> c1 = 0 # reset the count py> sorted(L, lambda a, b: cmp(abs(a), abs(b))) [1, -2, 2, 3, -3, 4, 5, -6, -7, -8, 9] py> c1, c2 (54, 27) The comparison function is called 27 times for a list of nine items (a average of 2.5 calls to cmp per item), and abs is called twice for each call to cmp. (Well, duh.) If the list is bigger, it gets worse: py> c2 = 0 py> x = sorted(L*10, lambda a, b: cmp(abs(a), abs(b))) py> c2 592 That's an average of 5.4 calls to cmp per item. And it gets even worse as the list gets bigger. As your lists get bigger, the amount of work done calling the comparison function gets ever bigger still. Sorting large lists with a comparison function is SLOOOW. py> del abs, cmp # remove the monkey-patched versions py> L = L*1000000 py> with Timer(): .... x = sorted(L, key=abs) .... time taken: 9.165448 seconds py> with Timer(): .... x = sorted(L, lambda a, b: cmp(abs(a), abs(b))) .... time taken: 63.579679 seconds The Timer() context manager used can be found here: [URL]http://code.activestate.com/recipes/577896[/URL] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
sort order for strings of digits
Top