in reply to The indisputable speed of tr///

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.

Replies are listed 'Best First'.
Re^2: The indisputable speed of tr///
by radiantmatrix (Parson) on Jun 26, 2006 at 20:29 UTC

    As for the my -> our change, thanks; I ran into and fixed that bug, must've pasted an old version. Fixed now.

    As for "first line", you'll note the commment above the DATA block that I added newlines to the data for posting on PM, but that the code I ran had *no* newlines in the DATA block. All the data was on the first line, in other words. Even if that were not the case, it wouldn't be buggy, it would just be testing less text.

    As for the "300x faster than", you're technically correct, it's "300x as fast as". However, you're also a pedant. ;-) Fixed, in any case.

    <radiant.matrix>
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet