In article <[EMAIL PROTECTED]>,
Michael G Schwern <[EMAIL PROTECTED]> wrote:
>
>so length() is very cheap. Unlike strlen() in C, it doesn't have to walk
>the string looking for a null byte.
Unless it is flagged as utf8, in which case it does have to walk the
string to count the length in *characters*.
>
>> If your interested, there's a few oddities I discovered en route too:
>> 1) The code:
>> next if $seen{$_}; $seen{$_}=1;
>> according to my benchmaring is faster than:
>> unless ($seen{$_}++) {...}
>> even though the former looks up $seen{$_} twice, and ++ is a pretty trivial
>> operator. Its not where I'd have put my money.
>
>Here's what I get on Linux.
Michael, your benchmark is flawed. If that ... above is more than one
statement, the { } with the unless version costs a lot more.
Nevertheless, with s/delete \$hash{foo}/$&; $&/ on the benchmark, I still
see unless slightly faster:
~ $perl5.8.0 bench.pl
Benchmark: running control, next, unless for at least 3 CPU seconds...
control: 4 wallclock secs ( 4.38 usr + 0.00 sys = 4.38 CPU) @ 642700.98/s
(n=2815673)
next: 3 wallclock secs ( 3.13 usr + 0.00 sys = 3.13 CPU) @ 121639.30/s
(n=380731)
unless: 4 wallclock secs ( 3.06 usr + 0.00 sys = 3.06 CPU) @ 214766.64/s
(n=658045)
Rate next unless control
next 121639/s -- -43% -81%
unless 214767/s 77% -- -67%
control 642701/s 428% 199% --
~ $perl5.8.0 corrected_bench.pl
Benchmark: running control, next, unless for at least 3 CPU seconds...
control: 4 wallclock secs ( 3.07 usr + 0.00 sys = 3.07 CPU) @ 631012.71/s
(n=1936578)
next: 3 wallclock secs ( 3.16 usr + 0.00 sys = 3.16 CPU) @ 109031.61/s
(n=344976)
unless: 3 wallclock secs ( 3.12 usr + 0.00 sys = 3.12 CPU) @ 136536.09/s
(n=425583)
Rate next unless control
next 109032/s -- -20% -83%
unless 136536/s 25% -- -78%
control 631013/s 479% 362% --