#!/usr/bin/perl use strict; use utf8; use Benchmark ':all'; # the pattern doesn't even need to contain anything fancy # for the problem to manifest itself my $pattern = 'dumbest pattern ever'; # it's all about whether or not the /i flag is embedded into the regex my $re = qr/$pattern/; my $re_i = qr/$pattern/i; # qr//i causes a noticeable slowdown even when dealing with 7-bit (US-ASCII) # strings, but this being multibyte seems to make things _a lot_ worse my $str = 'очень длинная строка ' x 10; my $count = 300_000; cmpthese($count, { 'qr//i+m//' => sub { $str =~ /$re_i/ }, 'qr//+m//i' => sub { $str =~ /$re/i }, 'qr//+m//' => sub { $str =~ /$re/ } }); #### $ ./qr-utf8.pl Rate qr//i+m// qr//+m//i qr//+m// qr//i+m// 10881/s -- -98% -98% qr//+m//i 505263/s 4543% -- -7% qr//+m// 540845/s 4870% 7% --