Skip to content

Instantly share code, notes, and snippets.

@Stantheman
Created March 14, 2013 02:42
Show Gist options
  • Save Stantheman/5158401 to your computer and use it in GitHub Desktop.
Save Stantheman/5158401 to your computer and use it in GitHub Desktop.
compare perl substitution against "chop if substr($var, -1) eq"
Benchmark: running sed, substr_if for at least 20 CPU seconds...
sed: 21 wallclock secs (21.10 usr + 0.00 sys = 21.10 CPU) @ 1937388.20/s (n=40878891)
substr_if: 21 wallclock secs (21.22 usr + 0.03 sys = 21.25 CPU) @ 2412531.58/s (n=51266296)
Rate sed substr_if
sed 1937388/s -- -20%
substr_if 2412532/s 25% --
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark qw( timethese cmpthese ) ;
my $r = timethese( -20, {
sed => sub {
my $sed_str = "This is a sentenceL";
$sed_str =~ s/.$//;
},
substr_if => sub {
my $substr_str = "This is a sentenceL";
chop $substr_str if (substr($substr_str, -1) eq 'L');
},
} );
cmpthese $r;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment