Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is the new <a href="http://perldoc.perl.org/perlop.html#Smartmatch-Operator" rel="noreferrer">smart match operator</a>:</p> <pre><code>#!/usr/bin/perl use 5.010; use strict; use warnings; my @x = (1, 2, 3); my @y = qw(1 2 3); say "[@x] and [@y] match" if @x ~~ @y; </code></pre> <p>Regarding <a href="http://search.cpan.org/perldoc/Array::Compare" rel="noreferrer">Array::Compare</a>:</p> <blockquote> <p>Internally the comparator compares the two arrays by using join to turn both arrays into strings and comparing the strings using <code>eq</code>.</p> </blockquote> <p>I guess that is a valid method, but so long as we are using string comparisons, I would much rather use something like:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use List::AllUtils qw( each_arrayref ); my @x = qw(1 2 3); my @y = (1, 2, 3); print "[@x] and [@y] match\n" if elementwise_eq( \(@x, @y) ); sub elementwise_eq { my ($xref, $yref) = @_; return unless @$xref == @$yref; my $it = each_arrayref($xref, $yref); while ( my ($x, $y) = $it-&gt;() ) { return unless $x eq $y; } return 1; } </code></pre> <p>If the arrays you are comparing are large, joining them is going to do a lot of work and consume a lot of memory than just comparing each element one by one.</p> <p><strong>Update:</strong> Of course, one should test such statements. Simple benchmarks:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use Array::Compare; use Benchmark qw( cmpthese ); use List::AllUtils qw( each_arrayref ); my @x = 1 .. 1_000; my @y = map { "$_" } 1 .. 1_000; my $comp = Array::Compare-&gt;new; cmpthese -5, { iterator =&gt; sub { my $r = elementwise_eq(\(@x, @y)) }, array_comp =&gt; sub { my $r = $comp-&gt;compare(\(@x, @y)) }, }; </code></pre> <p>This is the worst case scenario where <code>elementwise_eq</code> has to go through each and every element in both arrays 1_000 times and it shows:</p> <pre> Rate iterator array_comp iterator 246/s -- -75% array_comp 1002/s 308% -- </pre> <p>On the other hand, the best case scenario is:</p> <pre><code>my @x = map { rand } 1 .. 1_000; my @y = map { rand } 1 .. 1_000; </code></pre> <pre> Rate array_comp iterator array_comp 919/s -- -98% iterator 52600/s 5622% -- </pre> <p><code>iterator</code> performance drops quite quickly, however:</p> <pre><code>my @x = 1 .. 20, map { rand } 1 .. 1_000; my @y = 1 .. 20, map { rand } 1 .. 1_000; </code></pre> <pre> Rate iterator array_comp iterator 10014/s -- -23% array_comp 13071/s 31% -- </pre> <p>I did not look at memory utilization.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload