我正在尝试同时学习 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;
这在大约 3 秒内给出了 577,449 的总和。
我将如何重写它以利用 Perl 6 并行思想?我意识到节省的时间不会太多,但它可以作为概念证明。
最佳答案
实现克里斯托夫的建议。计数略高于我原来的帖子,因为我现在可以使用编码 latin1 读取格式错误的 UTF-8 文件。
use v6;
my $start_time = now;
my @files = "c:/iforms/live".IO.dir(test => / '.' htm $/);
my $total_lines = [+] @files.race.map(*.lines(:enc<latin1>).elems);
say $total_lines;
say now - $start_time;
关于parallel-processing - 在 Perl 6 中需要简单的并行示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34497164/