in reply to Short or Long Hand

After seeing a number of posts suggesting using a regex to accomplish this task, i am left a bit surprised. It seems to me that in terms of program speed, doing a couple numeric comparisons will be faster than a regex or two. This problem itself is somewhat trivial, but in general i see a lot of regex solutions here at perlmonks, which seem to be for the sake of terse code, but will probably take longer to execute in general than other alternatives.

Am i missing something here?

Replies are listed 'Best First'.
Re^2: Short or Long Hand
by hv (Prior) on Aug 12, 2004 at 16:22 UTC

    Regular expressions are attractive for this sort of problem, because they can express a number of constraints in a single pattern. In this case a regexp such as /[06]/ is slightly slower than the two numerical tests (at least on my local perl installation), but we're talking fractions of a microsecond, and the OP gives no reason to assume that speed is a concern.

    More relevant is what will make the code easier to read and maintain, and that usually means making the code reflect the reality behind the check as expressively as possible. Accordingly, I'd be tempted to write something like:

    use constant SATURDAY => 6; use constant SUNDAY => 0; if ($day != SATURDAY && $day != SUNDAY) { ... }
    .. but that might be overkill. :)

    Hugo

Re^2: Short or Long Hand
by etcshadow (Priest) on Aug 12, 2004 at 16:16 UTC
    i see a lot of regex solutions here at perlmonks, which seem to be for the sake of terse code, but will probably take longer to execute in general than other alternatives
    That's because computer time has a tendancy to be cheaper than programmer time. Of course, trading one hour of programmer time for a few days (or months or years) of computer time may well be worth it. This is the delicate balance of when optimization is worthwhile.
    ------------ :Wq Not an editor command: Wq