in reply to Re: Re^2: Replace zero-width grouping?
in thread Replace zero-width grouping?
It's the localization of the glob that somehow messes things up (actually, it appears to happen when Perl tries to restore it upon exiting the block). It doesn't really matter though. The principle is exactly the same as your "dio3" code, just more convoluted.
My intuition says the crucial bottleneck is the code assertion in the pattern, rather than the math. "buk3" beats "dio3" mostly, and the former certainly does more math than the latter.
One thing that occured to me is that when you have 4 substitutions in a row, you can skip the next 4 characters, since they'll all have been replaced. I can't think of a simple way to account for this fact in code though. Without a working single pattern solution it would require a nested loop, which is most probably not worth the effort. And if we had a working single pattern solution, there'd not probably be any manual optimization that could beat just letting the regex engine do its job.
Update: thinking about this gave me an idea.
#!/usr/bin/perl -wl use strict; my @ari; my $str = "17341234173412341734123417341234"; $ari[2] = $str; substr($ari[2], $_*4, 8) =~ s[(.)(?=...\1)|(?<=(.)...)\2]{ defined $1 ? "A" : "B" }eg for 0 .. length($ari[2])/4; print $ari[2]; __END__ A7AAB2BBA7AAB2BBA7AAB2BBA7AAB2BB
(Strange variable names left intact for easy addition to your benchmark.) It is consistently 4% faster than "buk3" on my machine.
All that said and done, unless I had a real performance problem with this task, I'd use diotalevi's initial simpleminded approach in production. It works correctly and is far easier to read than any of the alternatives.
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re^4: Replace zero-width grouping?
by BrowserUk (Patriarch) on May 09, 2003 at 07:24 UTC | |
by Aristotle (Chancellor) on May 09, 2003 at 09:55 UTC | |
by diotalevi (Canon) on May 09, 2003 at 13:41 UTC | |
by Aristotle (Chancellor) on May 09, 2003 at 18:58 UTC | |
by diotalevi (Canon) on May 09, 2003 at 19:14 UTC | |
by BrowserUk (Patriarch) on May 09, 2003 at 15:37 UTC | |
by hv (Prior) on May 09, 2003 at 18:14 UTC | |
by BrowserUk (Patriarch) on May 09, 2003 at 18:23 UTC |