Closest-pair problem: Difference between revisions

m
→‎{{header|Raku}}: concurrency speeds things up considerably
m (→‎{{header|Raku}}: eliminate a pair of temporary variables, add output)
m (→‎{{header|Raku}}: concurrency speeds things up considerably)
Line 4,058:
{{trans|Perl 5}}
 
Using concurrency, the 'simple' routine beats the (supposedly) more efficient one for all but the smallest sets of input.
We avoid taking square roots in the slow method because the squares are just as comparable.
(This doesn't always work in the fast method because of distance assumptions in the algorithm.)
<lang perl6>sub MAIN ($N = 5000) {
my @points = (^$N).map: { [rand × 20 - 10, rand × 20 - 10] }
 
printf "%.8f between (%.5f, %.5f), (%.5f, %.5f)\n", $_[2], @($_[1]), @($_[0]) for
my @candidates = closest-pair(@points.sort(*.[0]).rotor( 10 => -2, :partial).race.map: { closest-pair-simple(@points$_) }
say 'simple ' ~ (@candidates.sort: *.[2]).head(1).gist;
@candidates = @points.sort(*.[0]).rotor( 10 => -2, :partial).race.map: { closest-pair(@$_) }
say 'real ' ~ (@candidates.sort: *.[2]).head(1).gist;
}
 
Line 4,107 ⟶ 4,109:
}</lang>
{{out}}
<pre>simple (([-1.1560800527301716 -9.214015073077793] [-1.1570263876019649 -9.213340680530798] 0.0011620477602117762))
<pre>0.00805011 between (7.65892, 1.46308), (7.66695, 1.46366)
real (([-1.1570263876019649 -9.213340680530798] [-1.1560800527301716 -9.214015073077793] 0.0011620477602117762))</pre>
0.00805011 between (7.65892, 1.46308), (7.66695, 1.46366)</pre>
 
=={{header|REXX}}==
2,392

edits