Is there any way I could optimize the following script to run faster?
foreach my $arg (@data){ #
@score=();
`program $arg $arg1 > $result`; #!!! $arg1 is a very large file with lots of data!!!
open(FH,$result);
while(<FH>){
chomp;
if($_ =~ /\d+.+\s+(\d+\.\d+|\d+\.|\.\d+).+/){ #here i'm looking for any number such as: 21.343 or 12 or 0.22 or -3.0
push(@score, $1);
}
}
close FH;
@sorted = sort{$a <=> $b} @score; #a sorted score is what i actually want
}
There are a few things I can see (for instance not loading your result into the file immediately), but I suspect the main performance benefit you will get will probably be from using a different regex. To that end, do you have a better idea what the data output format from your program is?
Here’s some sample perl that may run a little bit quicker:
Notice a few things here:
The other people have said to use threads. You DO NOT need to do this, as running the process as I have done with the trailing pipe (|) in the open function causes perl to fork a process for you. Then you use standard unix pipes to read from the program asynchronously.