【问题标题】:Behaviour of feed operator pipeline馈送操作员管道的行为
【发布时间】: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


【解决方案1】:

slurpy 参数正在等待消耗传递给它的整个序列。考虑以下之间的区别:

perl6 -e 'sub foo($) { }; my $items := gather for 1..Inf { say $_ }; foo($items);'
perl6 -e 'sub foo(*@) { }; my $items := gather for 1..Inf { say $_ }; foo($items);'

第一个示例将完成,第二个示例将永远不会完成。因此,您可能希望将阶段功能更改为:

sub stage( &code, $elements --> Seq) {
    map(&code,  $elements);
}

【讨论】:

  • 我使用 slurpy 的原因是为了匹配 mapdocs.perl6.org/routine/map#class_List 的签名。我删除了 slurpy 并且执行时间现在是一致的!谢谢。但是,在前两个阶段完成之前,第三阶段仍然不会执行。
  • @drclaw Itemization,可能就像我上面评论的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 2018-08-15
  • 2012-03-17
相关资源
最近更新 更多