#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my @Patterns=("xxx", "SSS", "s:S"); my %master; cmpthese(50, { orig=>\&getCsvHash1, swap=>\&getCsvHash2, regex=>\&getCsvHash3, }); sub getCsvHash1 { %master=(); # unset the HASH. foreach my $wlp (@Patterns) { my $key=$wlp; $key =~ s/[\s+|:]/_/g; open FILE, "csv_file.csv" or die $!; while() { my $line=$_; { my @csv = split(",", $line); if ($csv[1] =~ /"$wlp/) { push (@{$master{$key}}, $line); # push as value of a hash- } } } #while() ends here close FILE; } #foreach $wlp ends here } #Function getWhiteListCsvArrays ends here. sub getCsvHash2 { %master=(); # unset the HASH. open FILE, "csv_file.csv" or die $!; while() { my $line=$_; foreach my $wlp (@Patterns) { my $key=$wlp; $key =~ s/[\s+|:]/_/g; { my @csv = split(",", $line); if ($csv[1] =~ /"$wlp/) { push (@{$master{$key}}, $line); # push as value of a hash- } } } } close FILE; } sub getCsvHash3 { %master=(); # unset the HASH. open FILE, "csv_file.csv" or die $!; while() { my $line=$_; my @csv = split(",", $line); if ($csv[1] =~ /xxx/) { push @{$master{$1}}, $line; } if ($csv[1] =~ /SSS/) { push @{$master{$1}}, $line; } if ($csv[1] =~ /s:S/) { push @{$master{s_S}}, $line; } } close FILE; } #### $ time perl pm923771.pl Rate swap orig regex swap 0.544/s -- -47% -81% orig 1.03/s 89% -- -64% regex 2.87/s 427% 178% -- real 2m38.179s user 2m34.503s sys 0m3.463s