Hi
I did a little benchmarking in regard to the
string manipulation issue raised on this forum.
Here are relevant parameters and benchmark results.
Problem
Given a string and a pattern, construct new string
by removing part of the string equal to pattern.
Remove only first occurrence of the pattern.
Problem solutions
Solution #1 ($x = $a) =~ s/\Q$b//;
Solution #2 $x = substr($a,0,index($a,$b)).substr($a,index($a,$b)+length($b));
$a - string
$b - pattern
$x - result
Benchmark infrastructure
HW - Standalone laptop PIII, 512M
OS - Windows 2000 Professional SP3
Perl - ActivePerl 5.8.0 Build 804
Benchmark technology
Randomly generated string and pattern from the
allowable set of characters.
Pattern starts and ends at random defined positions
within the string.
Business rules
- Allowable set of characters: a-z, A-Z, 0-9
- String length 50 characters
- Pattern variable length
- Match always exists
Result precision
Seconds * 1/1000000
Result rule
Cumulative time for direct string manipulation
not including infrastructure processing time
(random generation string and pattern, etc).
Number of iterations
- 10000
- 100000
- 1000000
- 10000000
Final results
# iterations Solution #1 Solution #2
(sec) (sec)
10000 0.330479 0.140206
100000 2.894159 1.231760
1000000 30.223414 10.805546
10000000 299.560781 106.883615
Cheers
Pavle