#!/usr/bin/perl -w use strict; use Benchmark 'cmpthese'; my $string = "this is a string" x 300; my $short_string = "this is a short string"; cmpthese(10000000, { 'index' => sub { my $res; $res = index($string, "this", 0); $res = index($string, "string"); $res = rindex($string, "string"); }, regex => sub { my $res; $res = $string =~ /^this/; $res = $string =~ /string/; $res = $string =~ /string$/; }, shortindex => sub { my $res; $res = index($short_string, "this", 0); $res = index($short_string, "short"); $res = rindex($short_string, "string"); }, shortregex => sub { my $res; $res = $short_string =~ /^this/; $res = $short_string =~ /short/; $res = $short_string =~ /string$/; }, substrindex => sub { my $res; my $substr = "this is"; $res = index($short_string, $substr); }, substrregex => sub { my $res; my $substr = "this is"; $res = $short_string =~ /\Q$substr\E/; } });