[issue11707] Create C version of functools.cmp_to_key()

Filip Gruszczyński report at bugs.python.org
Mon Apr 4 23:07:18 CEST 2011


Filip Gruszczyński <gruszczy at gmail.com> added the comment:

Here are some example performance results:

def cmp(x, y):
    return y - x
    
sorted(range(1, 10000000), key=cmp_to_key(cmp))

'''
C version:
real	0m19.994s
user	0m8.053s
sys	0m1.044s

Python version:
real	0m28.825s
user	0m28.046s
sys	0m0.728s

'''


def cmp(x, y):
    x = int(x)
    y = int(y)
    return (x > y) - (y > x)
    
sorted([str(i) for i in reversed(range(1, 2000000))], key=cmp_to_key(cmp))

'''
Python version

real	0m15.930s
user	0m15.629s
sys	0m0.284s

C version

real	0m10.880s
user	0m10.585s
sys	0m0.284s
'''

There is some performance gain. I don't know however, if it's enough to use C version instead of Python, that's for Raymond to decide.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11707>
_______________________________________


More information about the Python-bugs-list mailing list