Keystroke has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use Benchmark qw(cmpthese); my $string = '0'x13; cmpthese(0, { 'eq_loop' => q(for (0..12) { next if ($string eq '0000000000000'); $_ = ($string =~ tr/1/1/); }), 'tr_loop' => q(for (0..12) { next unless ($_ = ($string =~ tr/1/1/)); }), 'eq' => q($string eq '0000000000000';), 'tr' => q($string =~ tr/1/1/;), });
Benchmark: running eq, eq_loop, tr, tr_loop, each for at least 3 CPU seconds...
eq: 4 wallclock secs ( 3.30 usr + 0.00 sys = 3.30 CPU) @ 1173221.21/s (n=3871630)
eq_loop: 4 wallclock secs ( 3.00 usr + 0.00 sys = 3.00 CPU) @ 16577.00/s (n=49731)
tr: 3 wallclock secs ( 3.08 usr + 0.00 sys = 3.08 CPU) @ 536228.57/s (n=1651584)
tr_loop: 4 wallclock secs ( 3.17 usr + 0.00 sys = 3.17 CPU) @ 18796.53/s (n=59585)
Rate eq_loop tr_loop tr eq
eq_loop 16577/s -- -12% -97% -99%
tr_loop 18797/s 13% -- -96% -98%
tr 536229/s 3135% 2753% -- -54%
eq 1173221/s 6977% 6142% 119% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: tr faster than eq?
by hv (Prior) on Mar 10, 2005 at 17:57 UTC | |
by Keystroke (Scribe) on Mar 10, 2005 at 18:37 UTC | |
by Anonymous Monk on Mar 11, 2005 at 09:15 UTC | |
|
Re: tr faster than eq?
by thor (Priest) on Mar 10, 2005 at 17:22 UTC | |
by ikegami (Patriarch) on Mar 10, 2005 at 18:29 UTC | |
by thor (Priest) on Mar 10, 2005 at 19:22 UTC | |
by ikegami (Patriarch) on Mar 10, 2005 at 19:41 UTC | |
by Anonymous Monk on Mar 11, 2005 at 09:23 UTC | |
|
Re: tr faster than eq?
by neosamuri (Friar) on Mar 10, 2005 at 23:55 UTC | |
|
Re: tr faster than eq?
by Elijah (Hermit) on Mar 10, 2005 at 23:22 UTC | |
by davido (Cardinal) on Mar 11, 2005 at 06:33 UTC |