Mapping..... (Was Stonehenge)
Gary Ansok
gansok at digisle.net
Wed Aug 30 16:58:21 CDT 2000
Brent Fulgham wrote:
> Anyway, Perl 5.6 apparently changes things a bit so you can't modify an
> unnamed
> construct list (which is basically a constant) in-place. So the fix is to
> say:
>
> my $bar = join "|", map { my $val = $_; chop $val; $val}
> (
> "anathema", "bema", "carcinoma", "charisma", "diploma",
> "dogma", "drama", "edema", "enema", "enigma", "lemma",
> "lymphoma", "magma", "melisma", "miasma", "oedema",
> "sarcoma", "schema", "soma", "stigma", "stoma", "trauma",
> "gumma", "pragma",
> );
Which is another way of saying
my $bar = join "|", map { substr $_, 0, -1 }
( ... list ... )
Benchmarking shows that the substr() version is about 50% faster
than the chop() version -- and I think the substr() version is
easier to understand, too.
The old chop() version (without the temporary) was pretty close to
the substr() version -- I showed substr() just a little faster, but
wouldn't be surprised if chop() was faster in a different environment.
Even easier to understand might be { substr $_, 0, length($_) - 1 },
but that does impose a speed penalty and isn't nearly as Perlish.
> Which just introduces a new temporary variable. Is it less efficient? Who
> knows.
That's what Benchmark.pm is for!
Benchmark: timing 100000 iterations of chop, regexp, sublen, subneg...
chop: 13 wallclock secs (12.51 usr + 0.00 sys = 12.51 CPU) @
7994.88/s (n=100000)
regexp: 14 wallclock secs (13.28 usr + 0.00 sys = 13.28 CPU) @
7531.25/s (n=100000)
sublen: 10 wallclock secs ( 9.60 usr + 0.00 sys = 9.60 CPU) @
10413.41/s (n=100000)
subneg: 7 wallclock secs ( 7.88 usr + 0.00 sys = 7.88 CPU) @
12687.14/s (n=100000)
(regexp was { /(.*).\z/ }; sublen was { substr $_, 0, (length($_) - 1)
})
> I sent a funny e-mail to Damian about the fix:
>
> --------------------------------
> Tension mounting. Stress.
> Broken Coy is bad karma!
> Where can I find peace?
>
> It seems that Perl five
> point six broke your Coy module.
> Attached is a patch.
>
> You will, no doubt, need
> to make it harmonious
> with the rest of Coy.
>
> Water falls on a rock.
> A small gift from neophyte
> to the great teacher.
>
> ---------------------------------
>
> It was a great honor to get an e-mail back from him
> later this same day:
>
> When the student can
> patch the teacher's broken code,
> who is the master?
>
> Many thanks, grasshopper.
>
> Damian
LOL!
-- Gary
More information about the Thousand-oaks-pm
mailing list