#!/usr/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; our $string = "abcd" x 1000; $string .= "e"; $string .= "abcd" x 1000; our $study = $string; our $pass = "abcde"; our $fail1 = "foo12"; our $fail2 = "abdce"; study $study; cmpthese(-1, { index_pass => 'index($string, $pass)', regex_pass => '$string =~ /$pass/', study_pass => '$study =~ /$pass/', }); print ("\n\n"); cmpthese(-1, { index_fail1 => 'index($string, $fail1)', index_fail2 => 'index($string, $fail2)', regex_fail1 => '$string =~ /$fail1/', regex_fail2 => '$string =~ /$fail2/', study_fail1 => '$study =~ /$fail1/', study_fail2 => '$study =~ /$fail2/', }); __END__ Rate index_pass study_pass regex_pass index_pass 38331/s -- -6% -69% study_pass 40960/s 7% -- -67% regex_pass 125463/s 227% 206% -- Rate index_fail2 study_fail2 index_fail1 regex_fail2 regex_fail1 study_fail1 index_fail2 27306/s -- -0% -48% -56% -64% -99% study_fail2 27307/s 0% -- -48% -56% -64% -99% index_fail1 52608/s 93% 93% -- -15% -31% -98% regex_fail2 61837/s 126% 126% 18% -- -19% -98% regex_fail1 75918/s 178% 178% 44% 23% -- -98% study_fail1 3412032/s 12396% 12395% 6386% 5418% 4394% --