Predz
Predz

Reputation: 201

Removing forks in a shell script so that it runs well in Cygwin

I'm trying to run a shell script on windows in Cygwin. The problem I'm having is that it runs extremely slowly in the following section of code. From a bit of googling, I believe its due to there being a large amount of fork() calls within the script and as windows has to use Cygwins emulation of this, it just slows to a crawl.

A typical scenario would be in Linux, the script would complete in < 10 seconds (depending on file size) but in Windows on Cygin for the same file it would take nearly 10 minutes.....

So the question is, how can i remove some of these forks and still have the script return the same output. I'm not expecting miracles but I'd like to cut that 10 minute wait time down a fair bit.

Thanks.

check_for_customization(){
  filename="$1"    
  extended_class_file="$2"
  grep "extends" "$filename" | grep "class" | grep -v -e '^\s*<!--' | while read line; do 
    classname="$(echo $line | perl -pe 's{^.*class\s*([^\s]+).*}{$1}')"
    extended_classname="$(echo $line | perl -pe 's{^.*extends\s*([^\s]+).*}{$1}')"

    case "$classname" in
    *"$extended_classname"*) echo "$filename"; echo "$extended_classname |$classname | $filename" >> "$extended_class_file";;
    esac
  done
}

Update: Changed the regex a bit and used a bit more perl:

check_for_customization(){
  filename="$1"    
  extended_class_file="$2"
  grep "^\(class\|\(.*\s\)*class\)\s.*\sextends\s\S*\(.*$\)" "$filename" | grep -v -e '^\s*<!--' | perl -pe 's{^.*class\s*([^\s]+).*extends\s*([^\s]+).*}{$1 $2}' | while read classname extended_classname; do
    case "$classname" in
    *"$extended_classname"*) echo  "$filename"; echo "$extended_classname | $classname | $filename" >> "$extended_class_file";;
    esac
  done
}

So, using the above code, the run time was reduced from about 8 minutes to 2.5 minutes. Quite an improvement.

If anybody can suggest any other changes I would appreciate it.

Upvotes: 1

Views: 105

Answers (1)

Armali
Armali

Reputation: 19395

Put more commands into one perl script, e. g.

check_for_customization(){
  filename="$1" extended_class_file="$2" perl -n - "$1" <<\EOF
next if /^\s*<!--/;
next unless /^.*class\s*([^\s]+).*/; $classname = $1;
next unless /^.*extends\s*([^\s]+).*/; $extended_classname = $1;
if (index($extended_classname, $classname) != -1)
{
    print "$ENV{filename}\n";
    open FILEOUT, ">>$ENV{extended_class_file}";
    print FILEOUT "$extended_classname |$classname | $ENV{filename}\n"
}
EOF
}

Upvotes: 3

Related Questions