in reply to coding style suggestion: (...)[1] vs. [...]->[1]

The second one allocates a whole array and then throws it away. The first one is handled by perl and may be optimized into something much more efficient than it looks. I would not use the second when the first is available. Incidentally you'll find that the following split// variant

my $string = "zero one two three"; $number = (split /\s+/, $string,3)[1];

is more efficient. It needs to be 2 more than the index you want access. One more for the 0/1 base correction, and one more so that the index you want doesnt include additional values afterwards.

A benchmark shows that the variant I posted above is around 300% faster than the array variant you posted.

use Benchmark 'cmpthese'; our $string = "zero one two three four five six seven eight nine te +n"; my $bench={ list => '$number = ( split /\\s+/, $string ) [1]', array => '$number = [ split /\\s+/, $string ]->[1]', list3 => '$number = ( split /\\s+/, $string,3) [1]', }; while (my ($key,$expr)=each %$bench) { printf "%s : %s\n",$key,eval($expr)||die $@; } for my $i (1..3) { cmpthese -3,$bench; sleep(1); } __END__ list : one list3 : one array : one Benchmark: running array, list, list3, each for at least 3 CPU seconds +... array: 3 wallclock secs ( 3.06 usr + 0.00 sys = 3.06 CPU) @ 54 +729.26/s (n=167581) list: 2 wallclock secs ( 3.24 usr + 0.00 sys = 3.24 CPU) @ 85 +398.45/s (n=276264) list3: 4 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @ 22 +4004.62/s (n=679182) Rate array list list3 array 54729/s -- -36% -76% list 85398/s 56% -- -62% list3 224005/s 309% 162% -- Benchmark: running array, list, list3, each for at least 3 CPU seconds +... array: 3 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 53 +543.26/s (n=166466) list: 3 wallclock secs ( 3.25 usr + 0.00 sys = 3.25 CPU) @ 85 +733.23/s (n=278633) list3: 4 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 22 +6976.21/s (n=705896) Rate array list list3 array 53543/s -- -38% -76% list 85733/s 60% -- -62% list3 226976/s 324% 165% -- Benchmark: running array, list, list3, each for at least 3 CPU seconds +... array: 3 wallclock secs ( 3.02 usr + 0.00 sys = 3.02 CPU) @ 55 +106.76/s (n=166202) list: 3 wallclock secs ( 3.05 usr + 0.00 sys = 3.05 CPU) @ 84 +117.16/s (n=256305) list3: 3 wallclock secs ( 3.14 usr + 0.00 sys = 3.14 CPU) @ 22 +3896.50/s (n=703035) Rate array list list3 array 55107/s -- -34% -75% list 84117/s 53% -- -62% list3 223896/s 306% 166% --

---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Replies are listed 'Best First'.
Re: Re: coding style suggestion
by BrowserUk (Patriarch) on Sep 19, 2003 at 12:01 UTC

    For ultimate speed, use the magic split regex ' '. Say what?

    my $number = ( split ' ', $string, 3 )[1];

    Saves another 1% to 10% or so and it's even easier to type, though a little harder to remember:)

    From perlfunc:split

    As a special case, specifying a PATTERN of space (' ') will split on white space just as split with no arguments does. Thus, split(' ') can be used to emulate awk's default behavior, whereas split(/ /) will give you as many null initial fields as there are leading spaces. A split on /\s+/ is like a split(' ') except that any leading whitespace produces a null first field. A split with no arguments really does a split(' ', $_) internally.

    The very fact that there is a special case made for the splitting of whitespace delimited fields suggests that someone felt that this was worthwhile optimising. Who knows, maybe that someone was even LW himself.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.