#! /usr/bin/perl use strict; use warnings; use 5.020; #use Getopt::Long; use Benchmark qw( cmpthese ); use constant { ELEMENTS => 1_000, ELEM_LEN => 20, CHARACTERS => [ 'a' .. 'z', 'A' .. 'Z', 0 .. 9 ], }; #> sub routines #> -------------------------------------------------------------------- sub fill_array { my $aRef = shift; my $chars = CHARACTERS; for ( my $i = 0; $i < ELEMENTS; $i++ ) { my $str; $str .= $chars->[ rand( @{$chars} ) ] for 1 .. ELEM_LEN; push @$aRef, $str; } } sub main { fill_array( \my @original ); cmpthese( -1, { copy_direct => sub { my @copy = @original; }, copy_c_loop => sub { my @copy; for ( my $i=0; $i<@original; $i++ ) { $copy[$i] = $original[$i] }; }, copy_c_loop_push => sub { my @copy; for ( my $i=0; $i<@original; $i++ ) { push @copy, $original[$i] }; }, copy_foreach => sub { my @copy; my $i = 0; for my $e ( @original ) { $copy[$i++] = $e; }; }, copy_foreach_push => sub { my @copy; for my $e ( @original ) { push @copy, $e; }; }, }); }; #> main programm #> -------------------------------------------------------------------- main(); __END__