in reply to File reading efficiency and other surly remarks

One thing to keep in mind is that while striving for efficiency is important, it waste time profitably spent elsewhere. If you have a script that runs twice as long as it could, what does it matter if it only runs once a month? I'm not saying that's the case here. It's just something I like to keep in mind.

Oftimes, while I find that I can write a more efficient regex than one I find in a program that I am working on, I ask myself two questions:

  1. Will it be harder to maintain?
  2. How necessary is optimizing the program?
Understanding optimization is important, but it's also important to understand when to optimize.

For example in one script, I had to write the following regex:

$input =~ /^(?:[^_]|_(?!${value}))+_${value},((?:[A-Z]\d{1,2},?)+).*/;
Because of the nature of the data, I could have written (untested):
$input =~ /$value,((?:\w\d{1,2},?)+)/;
The second regex may not be easy to understand, but it's a heck of a lot easier to understand then the first. If the script didn't require maximum efficiency, I would have chosen the second for maintainability.

Cheers,
Ovid

Replies are listed 'Best First'.
RE: (Ovid - when *not* to optimize) Re: File reading efficiency and other surly remarks
by tilly (Archbishop) on Aug 26, 2000 at 08:02 UTC
    How does the rant go?

    Don't prematurely optimize!
    Don't prematurely optimize!
    Don't prematurely optimize!

    Instead modularize your code and after the fact, if it matters, you are in a position to recognize the big scale issues and redesign. Optimizing your operations won't give you order of magnitude improvements. Fixing your algorithms will.