note
Desdinova
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
<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;
</CODE>
And got these results:
<CODE>
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)
</CODE>
I know this is not new information but i figured i'd post here to highlight what you are saying.<BR>
PS -- The bechmark method stolen from [Benchmarking your code]
<BR><BR>
<B>UPDATE:</B> [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...<BR>
For a fairer test I compared a single char substituion with tr/// and s///
<CODE>
my $data = 'for-bar-baz';
$data =~ s/-/_/g;
print $data;
my $data = 'for-bar-baz';
$data =~tr/-/_/;
print $data;
</CODE>
Using the benchmarking above I got hese results:
<CODE>
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)
</CODE>
Still there is an advantage to tr/// over s/// which can be more noticable depending on your data.<BR><BR>
<STRONG> Update 2: </STRONG> [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.
9096
9096