Interestiing. Though I think your conclusion regarding "if the speed difference isn't relevant" is wrong. For instance, your test data had 2 "'s in a short string. By the time this gets to 10 "s the speed advantage swings the other way. Even more interesting is that if the string has 0 "s, using substr to avoid the zwa's is twice as fast.
Why would you run it on a string that had no "s. Well, if you're processing large volumes of data into a database from a flat file then you need to check each line as you go. Some will have embedded "s but many won't. You could make the s/// conditional by checking first with m// or index, but just checking causes another pass to be made and costs more than using the simpler regex on the substr.
As Abigail said (again) recently, and has rightly pulled me up for before, you have to check a range inputs before you can make a judgement.
I love Perl. There's always another nuance to explore.
#! perl -slw use strict; use Benchmark qw[timethese cmpthese]; our $s = q["string (12" or 1 foot or 1') into 6" MySQL varchar"]; our $t = q["string (12 or 1 foot or 1') into 6 MySQL varchar"]; our ($sub,$zwa); my $tests = { zwax0 => q[ $zwa=$t; $zwa =~ s/(?<=.)"(?=.)/""/g; ], subx0 => q[ $sub=$t; substr($sub, 1, -1) =~ s/"/""/g; ], zwax1 => q[ $zwa=$s; $zwa =~ s/(?<=.)"(?=.)/""/g; ], subx1 => q[ $sub=$s; substr($sub, 1, -1) =~ s/"/""/g; ], zwax4 => q[ $zwa=$s x4; $zwa =~ s/(?<=.)"(?=.)/""/g; ], subx4 => q[ $sub=$s x4; substr($sub, 1, -1) =~ s/"/""/g; ], zwax10 => q[ $zwa=$s x10; $zwa =~ s/(?<=.)"(?=.)/""/g; ], subx10 => q[ $sub=$s x10; substr($sub, 1, -1) =~ s/"/""/g; ], }; timethese( 2, $tests ); print 'Results same' if $zwa eq $sub; cmpthese( 100000, $tests ); __DATA__ Results same Benchmark: timing 100000 iterations of subx0, subx1, subx10, subx4, zw +ax0, zwax1, zwax10, zwax4 ... subx0: 1 wallclock secs ( 0.60 usr + 0.00 sys = 0.60 CPU) @ 16 +6112.96/s (n=100000) subx1: 7 wallclock secs ( 5.71 usr + 0.00 sys = 5.71 CPU) @ 17 +516.20/s (n=100000) subx10: 25 wallclock secs (24.95 usr + 0.00 sys = 24.95 CPU) @ 40 +08.66/s (n=100000) subx4: 12 wallclock secs (12.77 usr + 0.00 sys = 12.77 CPU) @ 78 +32.08/s (n=100000) zwax0: 2 wallclock secs ( 1.34 usr + 0.00 sys = 1.34 CPU) @ 74 +515.65/s (n=100000) zwax1: 5 wallclock secs ( 4.64 usr + 0.00 sys = 4.64 CPU) @ 21 +570.32/s (n=100000) zwax10: 31 wallclock secs (30.02 usr + 0.00 sys = 30.02 CPU) @ 33 +30.67/s (n=100000) zwax4: 15 wallclock secs (13.69 usr + 0.00 sys = 13.69 CPU) @ 73 +04.60/s (n=100000) Rate zwax10 subx10 zwax4 subx4 subx1 zwax1 zwax0 subx +0 zwax10 3331/s -- -17% -54% -57% -81% -85% -96% -98 +% subx10 4009/s 20% -- -45% -49% -77% -81% -95% -98 +% zwax4 7305/s 119% 82% -- -7% -58% -66% -90% -96 +% subx4 7832/s 135% 95% 7% -- -55% -64% -89% -95 +% subx1 17516/s 426% 337% 140% 124% -- -19% -76% -89 +% zwax1 21570/s 548% 438% 195% 175% 23% -- -71% -87 +% zwax0 74516/s 2137% 1759% 920% 851% 325% 245% -- -55 +% subx0 166113/s 4887% 4044% 2174% 2021% 848% 670% 123% - +-
..and remember there are a lot of things monks are supposed to be but lazy is not one of them
In reply to Re: Re: Regex matching on anywhere but the beginning or end of a line
by BrowserUk
in thread Regex matching on anywhere but the beginning or end of a line
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |