Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Performance improvement by using $_ instead of named variables

by dsheroh (Prior)
on Nov 04, 2013 at 10:18 UTC ( #1061114=note: print w/replies, xml ) Need Help??

in reply to Performance improvement by using $_ instead of named variables

You haven't said anything about where the text is coming from or where it's going, but, as a general rule:

Processing is fast.
I/O is slow.

Unless you're doing some seriously heavy manipulation of the text, your program is probably spending most of its time reading in data from disk/network/database and writing the results back out. You can improve a bit on that (e.g., by reading data in the right-sized chunks instead of slurping the whole dataset at once or using indexing so that you don't have to read unused data at all), but, ultimately, your program can never be faster than the time it takes to complete its I/O operations.

As for the question itself, I do believe (but have not benchmarked to confirm) that using $_ is marginally faster than using a named variable. However, if your program is so performance-sensitive that this tiny, tiny micro-optimization matters, then Perl is most likely the wrong language for you to be using in the first place. Get something lower-level and compiled that doesn't waste CPU time on magic and DWIMmery.

  • Comment on Re: Performance improvement by using $_ instead of named variables

Replies are listed 'Best First'.
Re^2: Performance improvement by using $_ instead of named variables
by QM (Parson) on Nov 04, 2013 at 12:25 UTC
    Processing is fast. I/O is slow.

    Whenever someone asks me to look at their code for speedups, we often get into this discussion. For the more obstinate, I often suggest making a "do-nothing" version of their program that just reads in (files or whatever) and writes it back out again. Run this on a sample/benchmark input. We can then argue about Big-Oh numbers, and how this do-nothing version's input/output volume compares with a real example.

    Results are often along these lines: "The Do Nothing version took 5 minutes to read a bazillion records and write them out again. The real program is taking an extra 5 seconds on top of that (on average). If you really need a speedup, use faster storage media."

    I've always found Perl's IO to be very fast, roughly comparable to something written in C. To check, compare a "passthrough" script like:

    perl -pe '' >/dev/null

    Quantum Mechanics: The dreams stuff is made of

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1061114]
and the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-10-19 18:13 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (255 votes). Check out past polls.