I've got a couple of possibilities here for speeding up your code. Best performance thus far: a capturing regular expression:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw(cmpthese :hireswallclock);
my $coords = 'lon,lat,alt ' x 20000;
cmpthese(100, {
OP => sub {
my @list = split(/[, ]/,$coords);
my $size = @list;
my @poly = ();
for (my $i=0; $i<=($size-3); $i+=3)
{
my $lon = $list[$i+0];
my $lat = $list[$i+1];
my $point = [$lon,$lat];
push (@poly, $point);
}
},
double_split => sub {
my @list = split(' ',$coords);
my @poly;
$poly[@list-1] = 0;
for (@list) {
push (@poly, [ (split /,/, $_)[0,
+1] ]);
}
},
while_shift => sub {
my @list = split(/[, ]/,$coords);
my @poly = ();
while (@list) {
push @poly, [shift @list, shift @l
+ist];
shift @list;
}
},
regex => sub {
my @poly = ();
while ($coords =~ /([^,]+),([^,]+),[^\
+s]+ /g) {
push @poly, [$1,$2];
}
},
chromatic => sub { my @poly = map { [ (split /,/, $_)[
+0, 1] ] } split / /, $coords;},
jwkrahn => sub { my @poly = map [ ( split /,/ )[ 0, 1
+] ], split ' ', $coords;},
ike1 => sub { my @poly = map [ (split /,/, $_)[0, 1] ]
+, split ' ', $coords;},
ike2 => sub { my @poly = map [ /([^,]*),([^,]*)/ ], sp
+lit ' ', $coords; },
});
__END__
Rate OP while_shift double_split ike1 jwkrahn chromat
+ic ike2 regex
OP 6.14/s -- -17% -49% -50% -51% -5
+2% -54% -59%
while_shift 7.42/s 21% -- -38% -40% -40% -4
+2% -45% -51%
double_split 11.9/s 95% 61% -- -3% -4% -
+7% -11% -21%
ike1 12.3/s 101% 66% 3% -- -1% -
+4% -8% -18%
jwkrahn 12.4/s 103% 67% 4% 1% -- -
+3% -8% -18%
chromatic 12.9/s 109% 73% 8% 4% 3%
+-- -4% -15%
ike2 13.4/s 119% 81% 13% 9% 8%
+5% -- -11%
regex 15.1/s 147% 104% 27% 23% 22% 1
+8% 13% --
Update: Following ikegami's post below, I reran with more tests, closing some apps I had open and changing the spec to match the OP (originally ran for x 1000, whereas spec says "tens of thousands") and got the same ordering.
This is perl, v5.8.9 built for MSWin32-x86-multi-thread
(with 9 registered patches, see perl -V for more detail)
|