rmocster has asked for the wisdom of the Perl Monks concerning the following question:
Hello everyone, happy Friday.
Please optimize my code or give suggestion on how to do it. On a big array (35k elements), it take over 1.5 minute to compute. Any suggestion is greatly appreciated. I have included a section of my code and an example of the array all_listing (first line). Thank you.
@all_listing = qw ('folder/file.000001.jpg','folder/file.000002.jpg', +... , 'folder/file.039000.jpg','folder/file.040000.jpg'); foreach $kk (@all_listing) { next if $kk eq ''; # next on empty line next if ($kk =~ m|(.+)\/\..+| and $hidden); # omit hidden (do +t) files if ($kk =~ m|(.+)\/(\d+)\.(.+$)|) { # match image sequence wi +thout prefix $path = $1; $num = $2; $ext = $3; print "$path: $num.$ext\n" if ($debug); $pathpre = "$path/$ext"; # path and ext as the key ; ad +ding ext so that if there are 2 seq with the same prefix and diff ext +ension print ">> $pathpre\n" if ($debug); if (exists $seq{$pathpre}) { push (@range_tmp,$num); # add element to array } else { $seq{$pathpre} = $num; # create hash @range_tmp = $num; # empty array and add new elem +ent to it } $new_seq{$pathpre} = [@range_tmp]; # create new seq hash $ext{$pathpre} = $ext; # create hash of extension } elsif ($kk =~ m|(.+)\/(.+?)([\._]+)(\d+)\.(.+$)|) { # match + image sequence $path = $1; $pre = $2; $div = $3 ; $num = $4; $ext = $5; print "$path: $pre$div$num.$ext\n" if ($debug); $pathpre = "$path/$pre$div$ext"; # path, prefix, divide +r and ext as the key ; adding ext so that if there are 2 seq with the + same prefix and diff extension print ">> $pathpre\n" if ($debug); if (exists $seq{$pathpre}) { push (@range_tmp,$num); # add element to array } else { $seq{$pathpre} = $num; # create hash @range_tmp = $num; # empty array and add new elem +ent to it } $new_seq{$pathpre} = [@range_tmp]; # create new seq hash $ext{$pathpre} = $ext; # create hash of extension } elsif ($kk =~ m|(.+)\/(.+?)(\d+)\.(.+$)|) { # match most im +age sequence, except this 7R01.0118762.dpx (number before dot) ; abov +e regex takes care of this match $path = $1; $pre = $2; $num = $3; $ext = $4; print "$path: $pre$num.$ext\n" if ($debug); $pathpre = "$path/$pre$ext"; # path, prefix and ext as +the key ; adding ext so that if there are 2 seq with the same prefix +and diff extension print ">> $pathpre\n" if ($debug); if (exists $seq{$pathpre}) { push (@range_tmp,$num); # add element to array } else { $seq{$pathpre} = $num; # create hash @range_tmp = $num; # empty array and add new elem +ent to it } $new_seq{$pathpre} = [@range_tmp]; # create new seq hash $ext{$pathpre} = $ext; # create hash of extension } else { push (@no_match,$kk); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Optimize my foreach loop / code
by davido (Cardinal) on Aug 20, 2016 at 04:09 UTC | |
by rmocster (Novice) on Aug 25, 2016 at 22:00 UTC | |
by davido (Cardinal) on Aug 26, 2016 at 07:17 UTC | |
|
Re: Optimize my foreach loop / code
by Anonymous Monk on Aug 20, 2016 at 03:56 UTC | |
by rmocster (Novice) on Aug 25, 2016 at 20:31 UTC |