<?xml version="1.0" encoding="windows-1252"?>
<node id="64448" title="Re: Common Regex Gotchas" created="2001-03-14 17:41:46" updated="2005-07-27 07:49:37">
<type id="11">
note</type>
<author id="30204">
Desdinova</author>
<data>
<field name="doctext">
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
&lt;CODE&gt;
#!/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' =&gt; '&amp;One',
   'Method Two TR' =&gt; '&amp;Two',
   'Method Three s'=&gt; '&amp;Three'
   }
);

exit;
&lt;/CODE&gt;
And got these results:
&lt;CODE&gt;
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)
&lt;/CODE&gt;
I know this is not new information but i figured i'd post here to highlight what you are saying.&lt;BR&gt;
PS -- The bechmark method stolen from [Benchmarking your code]
&lt;BR&gt;&lt;BR&gt;
&lt;B&gt;UPDATE:&lt;/B&gt; [Xxaxx] pointed out to me in [id://68206|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...&lt;BR&gt;
For a fairer test I compared a single char substituion with tr/// and s/// 
&lt;CODE&gt;
 my $data = 'for-bar-baz';
   $data =~ s/-/_/g;
   print $data;
 my $data = 'for-bar-baz';
 $data =~tr/-/_/;
 print $data;
&lt;/CODE&gt;
Using the benchmarking above I got hese results:
&lt;CODE&gt;
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)
&lt;/CODE&gt;
Still there is an advantage to tr/// over s/// which can be more noticable depending on your data.&lt;BR&gt;&lt;BR&gt;
&lt;STRONG&gt; Update 2: &lt;/STRONG&gt; [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. 

</field>
<field name="root_node">
9096</field>
<field name="parent_node">
9096</field>
</data>
</node>
