Her*_*rby 6 parallel-processing perl6
我正在尝试同时学习Perl 6和并行/并发.
对于一个简单的学习练习,我有一个550'.htm'文件的文件夹,我想要所有这些文件中的代码行总和.到目前为止,我有这个:
use v6;
my $start_time = now;
my $exception;
my $total_lines = 0;
my @files = "c:/testdir".IO.dir(test => / '.' htm $/);
for @files -> $file {
$total_lines += $file.lines.elems;
CATCH {
default { $exception = $_; } #some of the files error out for malformed utf-8
}
}
say $total_lines;
say now - $start_time;
Run Code Online (Sandbox Code Playgroud)
这在大约3秒内得到577,449的总和.
我如何重写它以利用Perl 6并行思想?我意识到节省的时间不会太多,但它可以作为概念的证明.
Mik*_*ain -1
use v6;
my $start_time = now;
my $exception;
my @lines;
my $total_lines = 0;
my @files = "c:/testdir".IO.dir(test => / '.' htm $/);
await do for @files -> $file {
start {
@lines.push( $file.lines.elems );
CATCH {
default { $exception = $_; } #some of the files error out for malformed utf-8
}
}
}
$total_lines = [+] @lines;
say $total_lines;
say now - $start_time;
Run Code Online (Sandbox Code Playgroud)