http://www.perlmonks.org?node_id=1015925

austinj has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code where I loop through several files (approx 300) and get the corresponding values from them. this builds a large hash where the first key is the profile name, followed by the info you want. Unfortunately this slows down significantly when I run a large number of files. It runs approx 0.05 seconds per profile if I do < 30 files but if I do a large number (300+) it takes up to 1.0 seconds per profile. Is there a way I can initialize the hash or someway speed this up when it is running a large number? Thanks

foreach my $customProf (@{scalar2array($Settings->{custom_pl +ots}[$i][$t]{profiles})}){ if(exists $profEvntVar->{$customProf}){next;} my ($evnt_nums_ref,$var_names_ref,$variables_ref,$last_evn +t_name) = getvars($customProf); $profEvntVar->{$customProf}{events} = $evnt_nums_ref; #Li +nk profiles with their respective events $profEvntVar->{$customProf}{times} = $evnt_times_ref; #Li +nk profiles with their respective events $profEvntVar->{$customProf}{vars} = $var_names_ref; # +Link profiles with their respective variables $profEvntVar->{$customProf}{varsref} = $variables_ref; + #Link profiles with their respective variables $profEvntVar->{$customProf}{lastEvent} = $last_evnt_name; + #Link profiles with their respective variables } sub getvars{ my $profile = shift(@_); my @ptk_info = `<system_command_here>`; my $evnt_nums_ref; my $events_found = 0; my $vars; my $prof_var_names; my $last_evnt_name; foreach (@ptk_info){ if(/^\s*(\d*\.\d*)0\s-\s(.*?)\s*$/){ $evnt_nums_ref->{$2} = $1; $last_evnt_name = $2; } if($events_found == 1 && /^\b/){ push( @{$vars}, split(" ") ); } elsif($events_found == 0 && /^Events$/){ $events_found = 1; } } foreach (@{$vars}){ if(/(.{1,8})\S*:\S*/){ $prof_var_names->{$1} = $_; } else{ $prof_var_names->{$_} = $_; } } my @sorted_vars = sort { lc($a) cmp lc($b) } @{$vars}; return $evnt_nums_ref,$prof_var_names,\@sorted_vars,$last_evnt_name;