#!/usr/bin/perl # # $Id: re-sub-again 2592 2003-12-21 00:33:01Z jmates $ # # The author disclaims all copyrights and releases this document into # the public domain. # # More regex/substr battles. use Benchmark qw(cmpthese); my $foo; # tmp var to hold results my $f = "asdf"; # what we're looking for in $s my $s = "asdf basdfjkerjdsa"; # need to reset pos() for regex tests as otherwise the /g becomes very # "fast" ... cmpthese( -5, { 'substr' => '$foo = 1 if substr ($s, 0, length $f) eq $f', 'substrp' => 'pos($s) = 0; $foo = 1 if substr ($s, 0, length $f) eq $f', 're' => 'pos($s) = 0; $foo = 1 if $s =~ m/^$f/', 're-nopos' => '$foo = 1 if $s =~ m/^$f/', 'reg' => 'pos($s) = 0; $foo = 1 if $s =~ m/^$f/g', 'reg-nopos' => '$foo = 1 if $s =~ m/^$f/g', # 'reo' => 'pos($s) = 0; $foo = 1 if $s =~ m/^$f/o', # 'reog' => 'pos($s) = 0; $foo = 1 if $s =~ m/^$f/og', } );