Sort with extra variables
Alex Martelli
aleax at mac.com
Fri Mar 2 22:25:12 EST 2007
Thomas Dybdahl Ahle <lobais at gmail.com> wrote:
> 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 code.
>
> 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?
Maybe
moves.sort(key=functool.partial(board, table, ply), reverse=True)
might be a bit faster? Not sure, but maybe worth trying.
If not, a bit faster (nothing major) might be
def f(move, board=board, table=table, ply=ply):
return getMoveValue(board, table, ply, move)
the small advantage here would be to use the lexically scoped variable
lookup just once (at nested-def time) with the three names being then
looked up as locals in the len(moves) call to f...
Alex
More information about the Python-list
mailing list