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)

In reply to Re: Fastest way to turn coordinate list into array by kennethk
in thread Fastest way to turn coordinate list into array by brancusi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.