newdeveloper
newdeveloper

Reputation: 696

Perl script is running slow when calling bash

I am running a perl script that calls another script. Via command line. But it runs extremely slow. The back ticks makes it run via command line.

for my $results (@$data){
  `/home/data/push_to_web $results->{file}`;
}

If i run the same command via bash /home/data/push_to_web book.txt the same script runs extremely fast. If i build a bash file that contains

/home/data/push_to_web book_one.txt
/home/data/push_to_web book_two.txt
/home/data/push_to_web book_three.txt

The code executes extremely fast. Is there any secret to speeding perl up via another perl script.

Upvotes: 1

Views: 282

Answers (1)

nickcrabtree
nickcrabtree

Reputation: 358

Your perl script fires up a new bash shell for each element in the array, whereas running the commands in bash from a file doesn't have to create any new shells.

Depending on how many files you have, and what's in your bash startup files, this could add a significant overhead.

One option would be to build a list of semicolon-separated commands in the for loop, and then run one system command at the end to execute them all in one bash process.

Upvotes: 3

Related Questions