#!/usr/bin/perl use strict; use warnings; # use Benchmark qw(:all) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); # UnixOS my $str = " test of white space"; my $results = timethese(100000000, { 'regex' => sub { $str =~ /^\s/ }, 'substr' => sub { substr($str, 0, 1) eq ' ' }, 'ord' => sub { ord($str) == 32 }, }, 'none'); cmpthese( $results ); __END__ $ perl test.pl Rate regex substr ord regex 9225092/s -- -44% -87% substr 16556291/s 79% -- -77% ord 73529412/s 697% 344% -- #### my @strs = ("1234 pass 25 30 1", " pass 25 30 2", "1965 pass 35 45 1", " pass 35 45 2"); foreach my $sample (@strs) { if (ord($sample) == 32) { say "Matched: " . $sample; } } __END__ $ perl test.pl Matched: pass 25 30 2 Matched: pass 35 45 2