#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; # use Benchmark qw( timethese cmpthese ) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); # UnixOS my @array = ("91:", "86:", "184:", "430:", "391:", "254:", "121:", "192:", "404:", "12:", "87:", "638:", "417:", "129:", "549:", "548:", "122:", "443:", "378:", "365:", "665:", "148:", "185:", "88:", "629:", "637:", "149:", "625:", "635:", "627:", "650:", "468:", "92:", "618:", "212:", "85:", "628:", "171:", "649:", "15:", "61:", "169:", "104:", "202:", "523:", "60:", "672:", "291:", "658:", "59:", "547:", "491:", "234:", "411:", "620:", "581:", "414:", "14:", "412:", "416:", "345:", "626:", "457:", "72:", "384:", "371:", "9:", "580:", "436:", "356:", "385:", "58:", "669:", "388:", "386:", "390:", "636:", "619:", "16:", "413:", "17:", "524:", "579:", "624:", "90:", "471:", "410:", "551:", "289:", "387:", "531:", "64:", "166:", "211:", "467:", "415:", "232:", "550:", "362:", "375:", "401:", "359:", "372:", "398:", "360:", "364:", "399:", "403:", "373:", "377:", "18:", "118:", "585:", "427:", "424:", "586:", "469:", "425:", "429:", "13:", "423:", "500:", "62:", "109:", "19:", "539:", "499:", "532:", "400:", "63:", "361:", "374:", "73:", "449:", "175:", "426:", "89:", "507:", "397:", "389:", "582:", "475:", "20:", "22:", "541:", "492:", "503:", "555:", "595:", "596:", "450:", "23:", "611:", "509:", "3:", "485:", "24:", "438:", "442:", "440:", "484:", "117:", "32:", "437:", "31:", "663:", "339:", "535:", "21:", "470:", "439:", "525:", "172:", "40:", "65:", "487:", "50:", "517:", "597:", "545:", "516:", "402:", "347:", "614:", "540:", "613:", "346:", "67:", "363:", "583:", "376:", "428:", "71:", "615:", "332:", "271:", "5:", "508:", "74:"); my @preserved = @ARGV; sub subIndex { @ARGV = @preserved; # restore original @ARGV while (<>) { chomp; # say "$.\t$_"; my @line = split /\s+/; # Matches one or more white space # print Dumper \@line; foreach my $element (@line) { if (index($element, ':') != -1) { my $indx = index($element, ':'); my $match = substr $element, 0, ++$indx; if ( grep( /^$match$/, @array ) ) { # say "Found it: $element"; } } } } } sub subSplit { @ARGV = @preserved; # restore original @ARGV while (<>) { chomp; # say "$.\t$_"; my @line = split /\s+/; # Matches one or more white space # print Dumper \@line; foreach my $element (@line) { if (index($element, ':') != -1) { my @tmp = split /:/, $element; my $match = $tmp[0] . ':'; if ( grep( /^$match$/, @array ) ) { # say "Found it: $element"; } } } } } my $results = timethese(100000, { 'split' => \&subSplit, 'index' => \&subIndex, }, 'none'); cmpthese( $results ); __END__ $ perl test.pl in.txt Rate split index split 145/s -- -4% index 151/s 4% --