PackedArray 的快速列表产品标志?
作为我之前的问题的延续,Simon 的方法查找PackedArray 的列表乘积速度很快,但不适用于负值。
这可以通过 Abs 以最小的时间损失来“修复”,但是符号丢失了,所以我需要单独找到产品符号。
我尝试过的最快方法是 EvenQ @ Total @ UnitStep[-lst]
lst = RandomReal[{-2, 2}, 5000000];
Do[
EvenQ@Total@UnitStep[-lst],
{30}
] // Timing
Out[]= {3.062, Null}
有更快的方法吗?
As a continuation of my previous question, Simon's method to find the list product of a PackedArray is fast, but it does not work with negative values.
This can be "fixed" by Abs
with minimal time penalty, but the sign is lost, so I will need to find the product sign separately.
The fastest method that I tried is EvenQ @ Total @ UnitStep[-lst]
lst = RandomReal[{-2, 2}, 5000000];
Do[
EvenQ@Total@UnitStep[-lst],
{30}
] // Timing
Out[]= {3.062, Null}
Is there a faster way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有点迟到的帖子:如果您最终对速度感兴趣,使用 C 编译目标的
Compile
似乎比迄今为止发布的最快解决方案快大约两倍(Tally
- 基于Sign
):以下是我机器上的计时:
A bit late-to-the-party post: if you are ultimately interested in speed,
Compile
with the C compilation target seems to be about twice faster than the fastest solution posted so far (Tally
-Sign
based):Here are the timings on my machine:
这比您的解决方案快两倍多,除了使用
Rule@@@
提取相关术语的废话之外,我发现它更清楚 - 它只是计算每个符号的数字元素。比较时序(和输出)
This is a little over two times faster than your solution and apart from the nonsense of using
Rule@@@
to extract the relevant term, I find it more clear - it simply counts the number elements with each sign.To compare timings (and outputs)