Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

RE: (Aighearach) Re: Efficient Perl Programming

by Fastolfe (Vicar)
on Nov 10, 2000 at 19:05 UTC ( [id://40950]=note: print w/replies, xml ) Need Help??


in reply to (Aighearach) Re: Efficient Perl Programming
in thread Efficient Perl Programming

I was a little more interested in finding documentation (perldoc-style if nothing else), or a niche waiting to be filled by some useful documentation, on general optimization tips (e.g. use !/\S/ instead of /^\s*$/ and why). This information might still be useful to others though. Thanks.

Replies are listed 'Best First'.
Re: Efficient Perl Programming
by Dominus (Parson) on Nov 11, 2000 at 02:02 UTC
    Here's one rule you might like: It is almost always a mistake to use the non-greedy regex quantifiers like *? and +?.

    Typical code looks like this:

    $churchill = qq{"If I were your husband," he said, "I should drink + it."}; while ($churchill =~ /"(.*?)"/g) { print $1, "\n"; }

    The purpose of the (.*?) is to capture a quoted part of the string in $churchill. Beginners usually try (.*) which doesn't work, because it captures everything from the first quotation mark to the last, including the non-quoted part in between. So then they ask how to fix this and are advised to use (.*?) instead. This does work, but it's much slower than it needs to be. A faster solution is:

    while ($churchill =~ /"([^"]*)"/g) { print $1, "\n"; }

    This says that what you're interested in is a quote character, followed by a sequence of characters that are not quotes ([^"]) followed by another quote. The description of what you want is more exact, and it enables Perl to do the matching more efficiently.

    So a good rule of thumb is to avoid .*? wherever possible and to use something like [^"]* instead when you can.

(Aighearach) Re: Re: Efficient Perl Programming
by Aighearach (Initiate) on Nov 10, 2000 at 19:13 UTC
      I'm going to ignore the attitude for the moment. Perhaps you misunderstand what I was requesting.

      How do I work with Perl references? is to perldoc perlref as What are some approaches I can take while writing code to ensure it is efficient? is to _______________

      Lots of others have pointed out some very good books and other various online resources, which is basically what I was looking for. Yes, I see that your Devel::OpProf module can be useful in post-development optimization, but I was looking for something more along the lines of education. I apologize if that was unclear, but I don't appreciate the smart-alec attitude.

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found