Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Beginning to see the light....

by earthboundmisfit (Chaplain)
on Jul 11, 2001 at 22:24 UTC ( [id://95791]=perlmeditation: print w/replies, xml ) Need Help??

All day I've had that old Velvet Underground song stuck in my head as I slowly discover all the ways in which my former Perl coding practices sucked. One small example:

To iterate through my @test = ('this','that','the other','thing') I used to write:

foreach $num (@test) { print $test[$num] . "\n"; }

or even worse:

for($num=0;$num<@test;$num++) { print $test[$num] . "\n"; }
I now simply bang out: print $_ . "\n" foreach @test; The less typing the better. Noone can accuse me of not being lazy. Other examples that have really changed my Perl world view include Ovid's node Death to dot star!, which really got me thinking about character classes and tilly's unique way of calling subs.

Armed with this knowledge, I've been producing faster, more maintainable scripts for fun and profit. Many thanks to all the saints (and you lesser creatures, too) who toil to enlighten the flock.

Some people work very hard but still they never get it right
Well, I'm beginning to see the light

aside: Please do not ++ this node as it's not an XP trap. My sincerest thanks to the community.

Replies are listed 'Best First'.
Re: Beginning to see the light....
by lemming (Priest) on Jul 12, 2001 at 01:41 UTC
    Some benchmarks:
    #!/usr/bin/perl use strict; use warnings; use Benchmark; my @test= qw(this that what where who how why); my $results = timethese(50000, { 'map' => sub { print map ("$_\n", @test) }, 'printfor' => sub { print "$_\n" for @test; }, 'join' => sub { print join("\n", @test),"\n"; }, 'forprint' => sub { for (@test) {print "$_\n"; } }, 'plain' => sub { print @test; }, #plain is for additional test like abigail 'abigail' => sub { $,=$\=$/; print @test; }, }, 'none' ); Benchmark::cmpthese($results);
    My results:
    Rate map printfor forprint abigail join plai +n map 48544/s -- -2% -5% -52% -71% -72 +% printfor 49505/s 2% -- -3% -51% -70% -71 +% forprint 51020/s 5% 3% -- -50% -69% -70 +% abigail 102041/s 110% 106% 100% -- -39% -41 +% join 166667/s 243% 237% 227% 63% -- -3 +% plain 172414/s 255% 248% 238% 69% 3% - +-

    Update:
    Ok, so I plugged Abigail's code in as well and I would of thought it would be the fastest. As the test 'plain' shows it is, when you aren't slowed down by an assignment.

    Further update: Added a couple more tests and saw my problem.

Re: Beginning to see the light....
by myocom (Deacon) on Jul 11, 2001 at 22:30 UTC
    I now simply bang out:

    print $_ . "\n" foreach @test;

    Note that you could further reduce this to:

    print map("$_\n",@test);

    TIMTOWTDI, natch...

      Or print"$_\n"for@test;
        Same number of chars, but wins if you have more than one of such prints:
        $,=$\=$/;print@test;

        -- Abigail

      guys, didn't you forget about:print join("\n", @test);?

      and unless you're using the return values of map you shouldn't use it


      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/

        Not at all. When using debug prints, I use map and join with equal frequency (depending on my mood and probably depending on surrounding code).

        Also note that I am using the return values of map in my print statement. I wrote print map("$_\n",@test);. If I had written map(print("$_\n"),@test); that would've been map in void context.

Re: Beginning to see the light....
by particle (Vicar) on Jul 11, 2001 at 23:58 UTC
    and remember, , is faster than . in print.

    ~Particle

    nevermind the xp, remember the experience.

Re: Beginning to see the light....
by E-Bitch (Pilgrim) on Jul 11, 2001 at 23:42 UTC
    Hay higherups... you could almost make a node (thats what these are called right?) out of this, and let people post unwieldy code snippets and have others suggest trimmed down versions...
    just a thought

    thanks!
    E-Bitch

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://95791]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 09:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found