Bad test. Change
my $data = <DATA>;
to
our $data = <DATA>;
so your code can see the variable.
You'll see the error if you do:
'table' => 'use strict; my $a = rot13($data),"\n"', 'tr' => 'use strict; my $a = rot13tr($data),"\n"',
Oddly, the results show you didn't suffer from the bug when you ran your code. The code you posted code and the code you ran are not the same.
Buggy test. You only read in the first line of data.
Bad interpretation.
"nearly 300 times faster than"
should read
"nearly 200 times faster than"
or
"nearly 300 times the speed of"
( No, he was right. )
New results:
Benchmark: running table, tr, each for at least 3 CPU seconds... table: 4 wallclock secs ( 3.13 usr + 0.00 sys = 3.13 CPU) @ 33 +1.84/s (n=1040) tr: 3 wallclock secs ( 3.23 usr + 0.00 sys = 3.23 CPU) @ 65 +432.56/s (n=211020) Rate table tr table 332/s -- -99% tr 65433/s 19618% --
(tr is 100x faster than table.)
New Benmark code:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese ); my %rot = ( 'A' => 'N','B' => 'O','C' => 'P','D' => 'Q','E' => 'R','F' => 'S', 'G' => 'T','H' => 'U','I' => 'V','J' => 'W','K' => 'X','L' => 'Y', 'M' => 'Z','N' => 'A','O' => 'B','P' => 'C','Q' => 'D','R' => 'E', 'S' => 'F','T' => 'G','U' => 'H','V' => 'I','W' => 'J','X' => 'K', 'Y' => 'L','Z' => 'M','a' => 'n','b' => 'o','c' => 'p','d' => 'q', 'e' => 'r','f' => 's','g' => 't','h' => 'u','i' => 'v','j' => 'w', 'k' => 'x','l' => 'y','m' => 'z','n' => 'a','o' => 'b','p' => 'c', 'q' => 'd','r' => 'e','s' => 'f','t' => 'g','u' => 'h','v' => 'i', 'w' => 'j','x' => 'k','y' => 'l','z' => 'm', ); sub rot13 ($) { # as close as possible to source project's code join '', map { exists $rot{$_} ? $rot{$_} : $_ } split('',shift); } sub rot13tr ($) { (my $s = shift) =~ tr/a-zA-Z/n-za-mN-ZA-M/; $s; } our $data; { local $/; $data = <DATA>; } cmpthese(-3, { #table => 'use strict; use warnings; my $rv = rot13 (our $data); 1', #tr => 'use strict; use warnings; my $rv = rot13tr(our $data); 1', table => 'my $rv = rot13 ($data); 1', tr => 'my $rv = rot13tr($data); 1', }); __DATA__ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus luctus nulla sed tellus. Sed vitae sapien in elit vestibulum faucibus. Maecenas sollicitudin, magna quis vestibulum convallis, ligula nulla fringilla augue, nec rutrum dolor quam vel justo. Vivamus nisi odio, ullamcorper sed, venenatis id, imperdiet eleifend, neque. Vivamus justo felis, euismod in, convallis auctor, egestas id, purus. In rutrum nisi in lectus. Aliquam elementum placerat dui. Integer ut pede sit amet magna pulvinar placerat. Maecenas massa lorem, lobortis ut, adipiscing at, suscipit nec, lectus. Curabitur mattis adipiscing sem. Mauris pharetra vehicula eros. Sed pellentesque elit laoreet augue. Cras non tellus. Quisque volutpat lectus in sem. Fusce vulputate justo ut pede. Aliquam ante pede, tempor in, dictum in, blandit eu, sapien. Morbi vestibulum, metus eu auctor vulputate, nulla lectus condimentum nisi, ac pulvinar ligula nunc in felis. Curabitur id orci ac est luctus molestie.
In reply to Re: The indisputable speed of tr///
by ikegami
in thread The indisputable speed of tr///
by radiantmatrix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |