Sort with extra variables
Thomas Dybdahl Ahle
lobais at gmail.com
Sat Mar 3 00:11:56 CET 2007
Den Fri, 02 Mar 2007 20:33:45 +0100 skrev Diez B. Roggisch:
> Thomas Dybdahl Ahle schrieb:
>> I have a sort function in a python chess program. Currently it looks
>> like this:
>> def sortMoves (board, table, ply, moves):
>> f = lambda move: getMoveValue (board, table, ply, move)
>> moves.sort(key=f, reverse=True)
>> return moves
>> However I'd really like not to use the lambda, as it slows down the
>> I've thought about saving the extra variables in the global space, but
>> it really feals ugly.
>> Do you have any ideas how I can sort these moves the fastest?
> First of all, in your case it is somewhat strange to use
> f = lambda ...
> because then you could as well use
> def f(move):
Wouldn't that be just as slow?
> But that is just a general remark. Regarding the question: I don't see
> how that could possibly become faster without much more insight into
> what you are doing in getMoveValue. As it seems, it is dependend of a
> lot of factors that change often, so caching it isn't a real option. And
> I hope you are aware that the key-method is invoked only _once_ per
Yeah, key is a nice thing. My only problem is that I need these other
objects to generate the value, and I don't want to create a new function
In my profiling the functions with the lambda line says 860 cumtime and
getMoveValue says 580.
More information about the Python-list