进料操作员管道的行为

  • 本文关键字:管道 操作员 raku
  • 更新时间 :
  • 英文 :


我正在使用以下代码对馈电操作员进行实验:

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();
  • 任务子只是燃烧CPU周期并显示使用的线程的循环
  • 阶段子是地图sub
  • 的包装器
  • 供稿管道的每个步骤映射以调用CPU密集任务并进行计时。地图的结果是输入 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

我的包装器在地图上是否有问题,导致此行为?

在包装器中评估所需的时间明显长于直接调用地图。

任何帮助都将不胜感激。

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

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

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

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

最新更新