#!/usr/bin/perl use strict; use warnings; use Benchmark; our $loop_line = "static replace [something] and [another] thing"; our %replace = (something => "SOMETHING", another => "ANOTHER"); my ($a, $b, $c); timethese -1 => { lazy => '$a = $loop_line; $a =~ s/\[(.*?)\]/$replace{$1}/g', restrictive => '$b = $loop_line; $b =~ s/\[([^]]*)\]/$replace{$1}/g', copy => '$c = $loop_line;', }; __END__ Benchmark: running copy, lazy, restrictive for at least 1 CPU seconds... copy: 2 wallclock secs @ 3994936.79/s (n=4234633) lazy: 1 wallclock secs @ 121663.37/s (n= 122880) restrictive: 1 wallclock secs @ 117028.57/s (n= 122880)