in reply to Re: Concatenation ... Strings
in thread Changing a character in the middle of a word (was: Concatenation ... Strings)
$char = chop $string while length $string > 4;
But, that may not be a good way to do it, although it is One More Way To Do It :)
#!/usr/bin/perl -w use Benchmark; $string = "hello world I am a string of some sort of length"; timethese(150_000, { "match" => sub { ($char) = $string =~ /^.{4}(.)/;}, "unpack" => sub { ($char) = unpack("x4 A1", $string);}, "substr" => sub { ($char) = substr($string,4,1);}, "silly" => sub { local $string; $char = chop $string while length + $string > 4;}, });
I consistently get results like so:
Benchmark: timing 150000 iterations of match, silly, substr, unpack... match: 4 wallclock secs ( 2.78 usr + 0.01 sys = 2.79 CPU) @ 53 +763.44/s (n=150000) silly: -1 wallclock secs ( 0.26 usr + -0.10 sys = 0.16 CPU) @ 93 +7500.00/s (n=150000) (warning: too few iterations for a reliable count) substr: 0 wallclock secs ( 0.48 usr + 0.00 sys = 0.48 CPU) @ 31 +2500.00/s (n=150000) unpack: 2 wallclock secs ( 1.41 usr + 0.11 sys = 1.52 CPU) @ 98 +684.21/s (n=150000)
Cheers,
KM
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re:{3} Concatenation ... Strings
by jeroenes (Priest) on Apr 12, 2001 at 11:19 UTC | |
by KM (Priest) on Apr 12, 2001 at 16:11 UTC |