【发布时间】:2019-03-15 08:08:21
【问题描述】:
我正在使用以下代码对提要运算符进行试验:
my $limit=10000000;
my $list=(0,1...4);
sub task($_,$name) {
say "Start $name on $*THREAD";
loop ( my $i=0; $i < $limit; $i++ ){};
say "End $name";
Nil;
}
sub stage( &code, *@elements --> Seq) {
map(&code, @elements);
}
sub feeds() {
my @results;
$list
==> map ({ task($_, "stage 1"); say( now - ENTER now );$_+1})
==> map ({ task($_, "stage 2"); say( now - ENTER now );$_+1})
==> stage ({ task($_, "stage 3"); say( now - ENTER now );$_+1})
==> @results;
say @results;
}
feeds();
- task sub 只是一个循环来消耗 cpu 周期并显示使用的线程
- stage sub 是 map sub 的封装
- 馈送管道的每个步骤都映射到调用 CPU 密集型任务并对其进行计时。 map的结果是输入+1
运行时的输出为:
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.7286811
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.59053989
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.5955893
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.59050998
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.59472201
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.5968531
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.5917188
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.587358
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.58689858
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.59177099
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.8549498
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.8560015
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.77634317
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.6754558
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.672909
[3 4 5 6 7]
@result 的结果是正确的($list 的输入增加了 3 次)
前两个阶段的输出拉动/交替,但第三阶段直到所有输入到阶段 2 完成后才执行
我的 map sub 的包装器是否存在导致此行为的问题?
此外,在包装器中进行评估所需的时间比直接调用 map 的时间要长得多。
感谢任何帮助。
【问题讨论】:
-
如果你打高尔夫球,我会帮你的。有些事情可以消除,例如对线程的引用,因为它都在同一个线程中运行。此外,您返回的是 Seq;可能直到真正需要它们时才会逐项列出它们,这将是当您到达第三阶段时。我会说这是正常的 Seq 行为。
-
我想你想用
$elements替换@elements -
@ugexe 你成功了。将
*@elements更改为*$elements可以让第三阶段与其他阶段同步运行,而不是等到它们完成。如下所述,我试图匹配maps 签名,该签名使用*@(至少是我找到的文档)。如果您想发布答案,我会除外。谢谢 -
@jjmerelo 我的高尔夫比赛需要改进!我最初发帖时正冲出门。将来我会尝试更好地格式化我的问题。感谢您的反馈
标签: raku