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

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

Hi Monks,
I'm having a problem with the attached script. When calling the function two times in a row, the 2nd time is by far slower.
The memory usage stays roughly the same, and it does not seem to be an issue of paging either. I wonder if it has to do with some fragmentation?
Is my data structure "too complicated"??
I use it on an XP OS with 2G RAM. Perl 5.8.8 Build 819
#!/usr/bin/perl -w use strict; use warnings; CheckAllPairs(); CheckAllPairs(); sub CheckAllPairs{ my $abs_time_res=50; my $max_peak_time=2050; my $subj_num; my $region; my $region1; my $region2; my $time; my $peaks; my @act_list1; my @act_list2; my $i; my $j; my $key; my $delta; my %map; my $start_A; my $end_A; my $start_B; my $end_B; my @region_list = (1..38); my @main_subjects_list = (1..38); $start_A = (times)[0]; foreach $region (@region_list) { foreach $subj_num (@main_subjects_list){ for $time (0..$max_peak_time){ if(rand(1000)<3){ push(@{$peaks->{$region}{$subj +_num}}, $time); } } } } $end_A = (times)[0]; printf "Section A took %.4f CPU seconds\n", $end_A - $start_A; $start_B = (times)[0]; foreach $region1 (@region_list) { foreach $region2 (@region_list) { if($region2<$region1){ next; } $key=$region1 . ':' . $region2; foreach $subj_num (@main_subjects_list){ if(!exists($peaks->{$region1}{$subj_nu +m}) || !exists($peaks->{$region2}{$subj_num})){ next; } @act_list1=@{$peaks->{$region1}{$subj_ +num}}; @act_list2=@{$peaks->{$region2}{$subj_ +num}}; foreach $i (@act_list1){ foreach $j (@act_list2){ $time=int($i/$abs_time +_res); $delta=int(($j-$i)/$ab +s_time_res)+int($max_peak_time/$abs_time_res); $map{$key}{$subj_num}[ +$time][$delta]=1; } } } } } $end_B = (times)[0]; printf "Section B took %.4f CPU seconds\n", $end_B - $start_B; return; }