forked from gustafe/1brc-perl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_average_mce.pl
75 lines (63 loc) · 1.67 KB
/
calculate_average_mce.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use strict;
use warnings;
use v5.38;
use MCE::Loop;
MCE::Loop->init( max_workers => 8, use_slurpio => 1 );
my $file = $ARGV[0];
my $start = time();
my @result = mce_loop_f {
my ($mce, $slurp_ref, $chunk_id) = @_;
my %data;
open my $MEM_FH, '<', $slurp_ref;
binmode $MEM_FH, ':raw';
while (<$MEM_FH>) {
chomp;
my ( $city, $temp ) = split( /;/, $_, 2);
if ( my $d = $data{$city} )
{
if ($temp < $d->{min}) {
$d->{min} = $temp
} elsif ($temp > $d->{max}) {
$d->{max} = $temp
}
}
else
{
$data{$city} = { min => $temp, max => $temp };
}
my $sum = $data{$city}->{sum} // 0;
$data{$city}->{sum} = $sum + $temp;
my $n = $data{$city}->{n} // 0;
$data{$city}->{n} = $n + 1;
}
close $MEM_FH;
MCE->gather(\%data);
} $file;
my %final_data;
# Merge the results
for my $result (@result) {
while (my ($city, $data) = each %$result) {
if (my $d = $final_data{$city}) {
if ($data->{min} < $d->{min}) {
$d->{min} = $data->{min};
}
if ($data->{max} > $d->{max}) {
$d->{max} = $data->{max};
}
} else {
$final_data{$city} = $data;
}
}
}
# Print the final results
print "{ ";
for my $city ( sort keys %final_data ) {
my $data = delete $final_data{$city};
printf '%s=%.1f/%.1f/%.1f', $city, $data->{min}, $data->{sum} / $data->{n}, $data->{max};
print ", " if %final_data;
}
print " }\n";
my $end = time();
my $run_time = $end - $start;
print "Processed in $run_time seconds\n";
1;