用额外的变量排序 [英] Sort with extra variables

查看:77
本文介绍了用额外的变量排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python国际象棋程序中有一个排序功能。

目前看起来像这样:


f = lambda move:getMoveValue(board,table,ply,move)

moves.sort(key = f,reverse = True)

返回移动


但是我真的不想使用lambda,因为它会降低代码的速度。


我'我想过要在全球空间中保存额外的变量,但是它确实很难看。


你有什么想法我怎么能对这些动作进行排序最快的?

解决方案

Thomas Dybdahl Ahle schrieb:


我有一个排序功能在一个python国际象棋程序。

目前它看起来像这样:

def sortMoves(板,桌子,层,移动):

f = lambda move:getMoveValue(board,table,ply,move)

moves.sort(key = f,reverse = True)

返回动作


但是我真的不喜欢使用lambda,因为它减慢了代码。


我已经考虑过在全球范围内保存额外的变量了,但是它确实很难看。


你有什么想法我可以排序吗?这些动作最快?



首先,在你的情况下使用它有点奇怪


f = lambda ...


因为那样你就可以使用


def f(移动):

....


但这只是一个普遍的评论。关于这个问题:我没有看到

如果没有更多洞察

你在getMoveValue中做什么可能会变得更快。看起来,它依赖于很多经常变化的因素,因此缓存它并不是一个真正的选择。并且

我希望你知道只有_once_每个

列表项调用key-method!


因此它效率非常高。


Diez


Thomas Dybdahl Ahle< lo **** @ gmail.comwrites:


你有什么想法我怎样才能最快地对这些动作进行排序?



一个想法:如果你正在使用alpha-beta修剪,也许你可以使用

类似heapq而不是排序,因为很多时候你只需看看前几个动作(最好先订购)。


Thomas Dybdahl Ahle写道:


但是我真的不想使用lambda,因为它减慢了代码的b



您是否检查了减速程度是多少?


问候,

Bj?rn


-

BOFH借口#65:


系统需要重新启动


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?

解决方案

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 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?

First of all, in your case it is somewhat strange to use

f = lambda ...

because then you could as well use

def f(move):
....

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
list-item!

Thus it is pretty efficient.

Diez


Thomas Dybdahl Ahle <lo****@gmail.comwrites:

Do you have any ideas how I can sort these moves the fastest?

One idea: if you''re using alpha-beta pruning, maybe you can use
something like heapq instead of sorting, since a lot of the time you
only have to look at the first few moves (ordered best-first).


Thomas Dybdahl Ahle wrote:

However I''d really like not to use the lambda, as it slows down
the code.

Did you check how much the slowdown is?

Regards,
Bj?rn

--
BOFH excuse #65:

system needs to be rebooted


这篇关于用额外的变量排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆