in reply to Re: Writing a simple RSS feed 'grabber' with XML::Parser.
in thread Writing a simple RSS feed 'grabber' with XML::Parser.

And in today's "making Perl work a lot harder than you need to do", let's nominate the following entry:
my $ticker=['http://perlmonks.org/index.pl?node_id=30175&xmlstyle=rss' +, "http://rss.news.yahoo.com/rss/science"]->[rand 2];
So, we've asked Perl to construct an array, take a reference to it, then dereference that reference to pick out one of the items, then discard the reference, which then garbage-collects the array. All when we could have written that this way:
my $ticker=('http://perlmonks.org/index.pl?node_id=30175&xmlstyle=rss' +, "http://rss.news.yahoo.com/rss/science")[rand 2];
saving two characters of typing, and all that mess of creating the new array and reference and garbage collecting. We're simply constructing a list, then picking out an element of that list with a literal slice (a construct I suggested for Perl 3, by the way {grin}).

To optimize this further, I'd go with a qw for that first list:

my $ticker=(qw(http://perlmonks.org/index.pl?node_id=30175&xmlstyle=rs +s http://rss.news.yahoo.com/rss/science))[rand 2];
And in recent versions of Perl, you can even drop that outer set of parens:
my $ticker=qw(http://perlmonks.org/index.pl?node_id=30175&xmlstyle=rss http://rss.news.yahoo.com/rss/science)[rand 2];

I saw bracket-arrow-bracket as a "cute syntax" once. I'm trying to stomp it out, because there's an equivalent construct (as I showed) that is a lot less work for Perl. Please don't propogate "cute syntax" that is more expensive.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^3: Writing a simple RSS feed 'grabber' with XML::Parser.
by demerphq (Chancellor) on Oct 20, 2004 at 10:51 UTC

    Yep, guilty as charged. But lighten up a little. It was just a snippet to make things a little more interesting.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi

      Flux8


Re^3: Writing a simple RSS feed 'grabber' with XML::Parser.
by Juerd (Abbot) on Oct 20, 2004 at 10:47 UTC

    perl -MBenchmark=cmpthese -e'cmpthese -1, { cute => sub { [0,1]->[rand + 2] }, list => sub { qw(0 1)[rand 2] } }'
    Rate cute list cute 768000/s -- -74% list 2899719/s 278% --
    Yes, the list slice is much faster. But for something that probably runs only once in every 5 minutes, isn't 768000 per second fast enough? Optimizing seems premature here. For things like this, I am against choosing a particular language or syntax for its speed.

    I'm not saying that your reply is useless. It's important to know what code does and this information will certainly help some of the readers when they do have to optimize. But the code is written now and not much is gained by changing it, so I'd just let it be. Programmer time is still much more expensive than computer time.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Please don't trot this out as an "optimization". What I'm saying is that this "cute syntax" is in fact gaining nothing, and only loses. So, if the only thing it's optimized for is "cuteness", it doesn't belong in the standard idiom list.

      So, at worst, I'm saving typing. Typically, I'm also saving execution speed. Why would you have a complaint about that?

      And what about the "programmer time" to figure out what the "cute" construct does? There's a real expense there. The literal slice is well documented. The cute block-arrow-block form shows up more like an obfuscation than a clarification, costing clarity, but for what reward? None, ever. Why unnecessarily obfuscate your code?

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

Re^3: Writing a simple RSS feed 'grabber' with XML::Parser.
by Anonymous Monk on Oct 20, 2004 at 11:27 UTC
    at least you have your disclaimer to guard against anyone thinking that you're an ass...