List::Util is a core module (ships with Perl). It provides the mesh() function, which can be used as follows:
my @doubled = mesh \@array, \@array;
It turns out to be really quick.
#!/usr/bin/env perl use v5.36; use strict; use warnings; use Benchmark qw(cmpthese); use List::Util qw(mesh); my @array = ('a' .. 'z'); cmpthese( -5, { mapped => \&mapped, meshed => \&meshed, }, ); sub mapped { return [map {$_,$_} @array]; } sub meshed { return [mesh \@array, \@array]; }
The output:
Rate mapped meshed mapped 111262/s -- -51% meshed 226692/s 104% --
When I bump it up to 676 elements ('aa' .. 'zz'), the comparison is similar:
Rate mapped meshed mapped 4157/s -- -50% meshed 8262/s 99% --
With 11756 elements ('aaa' .. 'zzz') the ratio holds.
Dave
In reply to Re: How to duplicate every member of an array
by davido
in thread How to duplicate every member of an array
by soblanc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |