Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Rewrite Program Using Arrays

by mbethke (Hermit)
on Mar 25, 2012 at 16:28 UTC ( #961501=note: print w/replies, xml ) Need Help??

in reply to Rewrite Program Using Arrays

Pushing data around from one array to the other is very unlikely to make things faster, probably the opposite. One thing you could so is write foreach my $word (split $line) to avoid the extra array, but this is something I suspect the Perl compiler might optimize all by itself already, you have top try whether it makes a difference. hashDescend() as a simple code block would probably be slightly faster, but other than that there's little room for optimization as far as I can see. Is this really too slow for your job?

Replies are listed 'Best First'.
Re^2: Rewrite Program Using Arrays
by perl.j (Pilgrim) on Mar 25, 2012 at 20:54 UTC
    I know it might not make it faster, but it's still a learning experience... Can you tell me how to do it?

      Well, you could get rid of your loop by using map, but my benchmark shows no gain there. Eliminating some intermediate variables does speed things up, though. If you get rid of @words and $word completely, you'll gain some time, regardless of which looping method you use:

      #!/usr/bin/env perl use Modern::Perl; use Benchmark qw(:all); # create a line with words and spaces to split on my $line = ''; $line .= ('a', 'b', 'c', 'd', ' ')[rand(5)] for (1..1000); cmpthese( 100000, { 'loop with vars' => \&loopwithvars, 'loop sans vars' => \&loopsansvars, 'with map' => \&withmap, }); sub loopwithvars { my %wordcount; my @words = split ' ', $line; for my $word (@words){ $wordcount{$word}++; } } sub loopsansvars { my %wordcount; for (split ' ', $line){ $wordcount{$_}++; } } sub withmap { my %wordcount; map { $wordcount{$_}++ } split ' ', $line; } ###### results ###### Rate loop with vars with map loop sans vars loop with vars 11338/s -- -15% -17% with map 13369/s 18% -- -2% loop sans vars 13624/s 20% 2% --

      Aaron B.
      My Woefully Neglected Blog, where I occasionally mention Perl.

        Get rid of a scope, use preinc instead of postinc.

        sub ikegami { my %wordcount; ++$wordcount{$_} for split ' ', $line; }
                          Rate loop with vars      with map loop sans vars       ikegami
        loop with vars  8324/s             --          -19%           -20%          -22%
        with map       10241/s            23%            --            -1%           -4%
        loop sans vars 10370/s            25%            1%             --           -3%
        ikegami        10666/s            28%            4%             3%            --

        (Post-increment in void context is optimised to pre-increment, but it's not in void context in "with map".)

      Hm, I suppose you meant something like this:
      my @globalarray; foreach my $filename (@ARGV) { open my $fh, '<', $filename or die "$filename: $!"; my @temparray = <$fh>; push @globalarray, @temparray; close $fh; }
      Guess you could learn the use of "push", that's about the only thing. It's used to append a scalar or a whole list to the end of an array. "pop" does the opposite and takes one scalar off. shift and unshift work the same, just on the start of the array.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961501]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2017-02-25 14:56 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (365 votes). Check out past polls.