检查列表是否包含零的最快方法
在准备回答秘密圣诞老人 - 生成“有效”排列时,我遇到需要检查列表是否包含零。我想知道在 Mathematica 7 中执行此操作的最快方法是什么,重点是非负整数的简短列表。
使用 Min[list] === 0
是我发现的最快的,比 MemberQ
或 FreeQ
更快,但我希望有一个更快的方法,与下面的 BitXor
操作相当:
r = Range@9;
p = Permutations@r;
BitXor[r, #] & /@ p; // Timing
0 === Min[BitXor[r, #]] & /@ p; // Timing
{0.062, Null}
{0.452, Null}
While preparing an answer to Secret Santa - Generating 'valid' permutations I came across the need to check if a list contains zero. I am wondering what the fastest way to do this in Mathematica 7 is, with emphasis on a short list of non-negative integers.
Using Min[list] === 0
is the fastest I have found, faster than MemberQ
or FreeQ
, but I am hoping there is a faster way, on par with the BitXor
operation below:
r = Range@9;
p = Permutations@r;
BitXor[r, #] & /@ p; // Timing
0 === Min[BitXor[r, #]] & /@ p; // Timing
{0.062, Null}
{0.452, Null}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题中使用的
0 === Min
的减速似乎是对Map
自动编译的干扰。分离这些步骤几乎提供了双重改进:如果我接受结果为 (0|1) 并且如果我将列表作为一个处理,那么我可以使用以下命令:
It appears that part of the slowdown with
0 === Min
as used in the question is interference with the autocompile ofMap
. Separating these steps provides nearly a twofold improvement:If I accept a result as (0|1) and if I process the list as one then I can use this: