in reply to Common Regex Gotchas

Regarding the simple substitutions section just to prove your point about not going overkill i benchmarked the two ways you mentioned (tr and s) as well as just uc with this code
#!/usr/local/bin/perl -w use strict; use Benchmark; my $count =500000; ## Method number one sub One { my $data = 'for bar baz'; $data = uc $data; } ## Method number two sub Two { my $data = 'for bar baz'; $data =~ tr/a-z/A-Z/; } ## Method number Three sub Three { my $data = 'for bar baz'; $data =~ s/([A-Za-z]+)/uc($1)/ge; } ## We'll test each one, with simple labels timethese ( $count, {'Method One UC' => '&One', 'Method Two TR' => '&Two', 'Method Three s'=> '&Three' } ); exit;
And got these results:
Benchmark: timing 500000 iterations of Method One UC, Method Three s, +Method Two TR... Method One UC: 1 wallclock secs ( 1.42 usr + 0.00 sys = 1.42 CPU) @ + 352112.68/s (n=500000) Method Three s: 16 wallclock secs (17.03 usr + 0.00 sys = 17.03 CPU) +@ 29359.95/s (n=500000) Method Two TR: 1 wallclock secs ( 2.04 usr + 0.00 sys = 2.04 CPU) @ + 245098.04/s (n=500000)
I know this is not new information but i figured i'd post here to highlight what you are saying.
PS -- The bechmark method stolen from Benchmarking your code

UPDATE: Xxaxx pointed out to me in This Node That I am not making a fair comparision above. The eval of uc($1) in the s/// regex was eating up a lot of the cycles. The gap is smaller than 17:1 shown above...
For a fairer test I compared a single char substituion with tr/// and s///
my $data = 'for-bar-baz'; $data =~ s/-/_/g; print $data; my $data = 'for-bar-baz'; $data =~tr/-/_/; print $data;
Using the benchmarking above I got hese results:
Benchmark: timing 500000 iterations of Method One TR, Method Two s... Method One TR: 2 wallclock secs ( 1.87 usr + 0.00 sys = 1.87 CPU) @ + 267379.68/s (n=500000) Method Two s: 5 wallclock secs ( 4.84 usr + 0.00 sys = 4.84 CPU) @ +103305.79/s (n=500000)
Still there is an advantage to tr/// over s/// which can be more noticable depending on your data.

Update 2: petral asked me question in the CB about the way i call uc in method one made me realize that it wont actually do anything because I don't assign the return value back to the var. I updated the code to do that.