Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need simple parallelism example in Perl 6

I am trying to learn Perl 6 and parallelism/concurrency at the same time.

For a simple learning exercise, I have a folder of 550 '.htm' files and I want the total sum of lines of code among all of them. So far, I have this:

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;

That gives a sum of 577,449 in approximately 3 seconds.

How would I rewrite that to take advantage of Perl 6 parallelism ideas? I realize the time saved won't be much but it will work as proof of concept.

like image 752
Herby Avatar asked Dec 28 '15 16:12

Herby


1 Answers

Implementing Christoph's suggestion. The count is slightly higher than my original post because I'm now able to read in the malformed UTF-8 files using encode latin1.

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;
like image 173
Herby Avatar answered Oct 26 '22 04:10

Herby