#!/usr/bin/perl # # $Id: substr-vs-regex 2592 2003-12-21 00:33:01Z jmates $ # # The author disclaims all copyrights and releases this document into # the public domain. # # Comparison of substr versus regex for simple string operations. use Benchmark; $source = "ad2349t724j kh24k5j24^ 5khj4i25h8 235 :34678: yu8h2b5j3"; timethese(500000, { 'regex' => '$tmp = $source; $tmp =~ s/^...//; $tmp =~ s/...$//', 'substr' => '$tmp = $source; $tmp = substr $tmp, 3, -3;', 'bad_re' => '$tmp = $source; $tmp =~ s/^...(.*)...$/$1/;', 'stupid' => '$tmp = $source; @foo = split undef, $tmp; shift @foo for 1..3; pop @foo for 1..3; $tmp = join "", @foo;', });