Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Faster way to execute contents of a perl script?

Reply
Thread Tools

Faster way to execute contents of a perl script?

 
 
Brandon Hoppe
Guest
Posts: n/a
 
      03-10-2005

Hi,

I'm trying to find a faster way to do this. Right now I have a perl script that contains
function definitions. I have second perl script that contains calls to these functions.

Now the first perl script will execute the second perl script hundreds of times, in order
to call these functions.

So I have a loop that repeatadly changes global variables and then executes the second
perl script with a do $file; call.

This works and all, but its not very fast. It takes about 1 1/2 seconds to execute the
second script, so over hundreds of runs, you can see why it takes forever.

I was wondering if there's a faster way to repeatedly re-call all these functions. Like
reading the second script into an array and then "executing" the array. I doubt this is
possible but I was thinking something along this line, to get the file into memory to
execute over and over again which should be faster than to be reading from disk everytime.

Brandon
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      03-10-2005
In article <d0q1s7$1h9$(E-Mail Removed)>,
Brandon Hoppe <(E-Mail Removed)> wrote:
:I'm trying to find a faster way to do this. Right now I have a perl script that contains
:function definitions. I have second perl script that contains calls to these functions.

:Now the first perl script will execute the second perl script hundreds of times, in order
:to call these functions.

:So I have a loop that repeatadly changes global variables and then executes the second
erl script with a do $file; call.

Don't do that. Use 'use' instead to bring the functionality in once, and then
just call upon the routines at need. Don't worry, the global variable values that
will be picked up will be those at execution time, not those at compile time.

Read the documentation on 'use'; also I suggest you might as well go as far as
formatting it into a "module" -- perldoc perlmod

--
"This was a Golden Age, a time of high adventure, rich living and
hard dying... but nobody thought so." -- Alfred Bester, TSMD
 
Reply With Quote
 
 
 
 
Brandon Hoppe
Guest
Posts: n/a
 
      03-10-2005


Jim Gibson wrote:
> In article <d0q1s7$1h9$(E-Mail Removed)>, Brandon Hoppe
> <(E-Mail Removed)> wrote:
>
>
>>Hi,
>>
>>I'm trying to find a faster way to do this. Right now I have a perl script
>>that contains
>>function definitions. I have second perl script that contains calls to these
>>functions.
>>
>>Now the first perl script will execute the second perl script hundreds of
>>times, in order
>>to call these functions.
>>
>>So I have a loop that repeatadly changes global variables and then executes
>>the second
>>perl script with a do $file; call.
>>
>>This works and all, but its not very fast. It takes about 1 1/2 seconds to
>>execute the
>>second script, so over hundreds of runs, you can see why it takes forever.
>>
>>I was wondering if there's a faster way to repeatedly re-call all these
>>functions. Like
>>reading the second script into an array and then "executing" the array. I
>>doubt this is
>>possible but I was thinking something along this line, to get the file into
>>memory to
>>execute over and over again which should be faster than to be reading from disk everytime.

>
>
> There is no need to repeatedly 'do' the second file containing
> subroutines. After this has been done once, the subroutines may be
> called multiple times by name. Just make sure all of the code you wish
> to execute is really in a subroutine and not on the main branch in the
> second file.


The second file doesn't contain the subroutines, it contains CALLS to the subroutines.
That's why I have to keep 'do'ing the second file so that it calls the subroutines for me.
 
Reply With Quote
 
Brandon Hoppe
Guest
Posts: n/a
 
      03-10-2005


Walter Roberson wrote:
> In article <d0q1s7$1h9$(E-Mail Removed)>,
> Brandon Hoppe <(E-Mail Removed)> wrote:
> :I'm trying to find a faster way to do this. Right now I have a perl script that contains
> :function definitions. I have second perl script that contains calls to these functions.
>
> :Now the first perl script will execute the second perl script hundreds of times, in order
> :to call these functions.
>
> :So I have a loop that repeatadly changes global variables and then executes the second
> erl script with a do $file; call.
>
> Don't do that. Use 'use' instead to bring the functionality in once, and then
> just call upon the routines at need. Don't worry, the global variable values that
> will be picked up will be those at execution time, not those at compile time.
>
> Read the documentation on 'use'; also I suggest you might as well go as far as
> formatting it into a "module" -- perldoc perlmod
>


Perhaps an example will help to visualize the problem:

#MAIN PERL SCRIPT
#!/usr/local/perl

while(<INPUT>) {
# read some value from INPUT
$VALUE = $_;

do "file.pl";
}

sub function1 {
# do something
}

sub function2 {
# do something
}


#FILE.PL SCRIPT
if($VALUE eq "A") {
&function1(34, 44, 33);
&function2(22, 33, 44);
}
if($VALUE eq "B") {
&function1(34, 44, 33);
&function2(22, 33, 44);
}


As you can see, the main perl script has the subroutine definitions. Now based on some
logic, the main script will execute either scriptA or scriptB, which just has calls to the
subroutines.

Scripts A and B are used elsewhere in another flow, so I don't want to modify these files
or compile them since that would change their behavior. This main perl script is a QUALITY
CONTROL script that verifies that Scripts A and B have valid data.

I hope I've given enough information as to what I'm needing to do.
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      03-10-2005
In article <d0q7ff$4p9$(E-Mail Removed)>,
Brandon Hoppe <(E-Mail Removed)> wrote:
erhaps an example will help to visualize the problem:

:As you can see, the main perl script has the subroutine definitions. Now based on some
:logic, the main script will execute either scriptA or scriptB, which just has calls to the
:subroutines.

:Scripts A and B are used elsewhere in another flow, so I don't want to modify these files
r compile them since that would change their behavior.

I'm not sure why you say that compiling them would change their behaviour?

Perhaps you could proceed as follows:

sub make_sub($$) {
my ($subname, $filename) = @_;
open( SUB, $filename ) or die "Cannot find subroutine file $filename\n";
local $\;
$subfilecontent = <SUB>;
close( SUB );
eval "sub $subname() { $subfilecontent }";
}


This creates a subroutine on the fly that is does whatever the
given file would do, by importing the content of that file.
--
Usenet is like a slice of lemon, wrapped around a large gold brick.
 
Reply With Quote
 
Brandon Hoppe
Guest
Posts: n/a
 
      03-10-2005


Walter Roberson wrote:
> In article <d0q7ff$4p9$(E-Mail Removed)>,
> Brandon Hoppe <(E-Mail Removed)> wrote:
> erhaps an example will help to visualize the problem:
>
> :As you can see, the main perl script has the subroutine definitions. Now based on some
> :logic, the main script will execute either scriptA or scriptB, which just has calls to the
> :subroutines.
>
> :Scripts A and B are used elsewhere in another flow, so I don't want to modify these files
> r compile them since that would change their behavior.
>
> I'm not sure why you say that compiling them would change their behaviour?
>
> Perhaps you could proceed as follows:
>
> sub make_sub($$) {
> my ($subname, $filename) = @_;
> open( SUB, $filename ) or die "Cannot find subroutine file $filename\n";
> local $\;
> $subfilecontent = <SUB>;
> close( SUB );
> eval "sub $subname() { $subfilecontent }";
> }
>
>
> This creates a subroutine on the fly that is does whatever the
> given file would do, by importing the content of that file.


Bingo! I was trying to do this earlier but couldn't figure out how to create a new
subroutine to include everything in the subfile.

Only thing is that I had to change this line:

$subfilecontent = <SUB>;

to

@subfilecontent = <SUB>;

Thanks!
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      03-10-2005
In article <d0qajd$lcj$(E-Mail Removed)>,
Walter Roberson <(E-Mail Removed)-cnrc.gc.ca> wrote:
erhaps you could proceed as follows:

:sub make_sub($$) {
: my ($subname, $filename) = @_;
: open( SUB, $filename ) or die "Cannot find subroutine file $filename\n";
: local $\;

That should be local $/; instead.

: $subfilecontent = <SUB>;
: close( SUB );
: eval "sub $subname() { $subfilecontent }";
:}
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      03-11-2005
Walter Roberson wrote:

> In article <d0q7ff$4p9$(E-Mail Removed)>,
> Brandon Hoppe <(E-Mail Removed)> wrote:
> erhaps an example will help to visualize the problem:
>
> :As you can see, the main perl script has the subroutine definitions. Now based on some
> :logic, the main script will execute either scriptA or scriptB, which just has calls to the
> :subroutines.
>
> :Scripts A and B are used elsewhere in another flow, so I don't want to modify these files
> r compile them since that would change their behavior.
>
> I'm not sure why you say that compiling them would change their behaviour?
>
> Perhaps you could proceed as follows:
>
> sub make_sub($$) {
> my ($subname, $filename) = @_;
> open( SUB, $filename ) or die "Cannot find subroutine file $filename\n";
> local $\;
> $subfilecontent = <SUB>;
> close( SUB );
> eval "sub $subname() { $subfilecontent }";
> }
>
>
> This creates a subroutine on the fly that is does whatever the
> given file would do, by importing the content of that file.


Be aware that this approach will do odd things if the script has global
variables (either package scoped or file scoped lexicals).

The first step to aleviate this is to do as Apache::Registry does and
compile each such subroutine script in it's own package. You also
probably want at least some ruimentary error reporting.

eval "sub $subname() { package MyImportedScripts::$subname;\n#line 1
\"$filename\"\n $subfilecontent }; 1" or die $@;

(Obiously MyImportedScripts should really be a subpackage of a package
namespace your application already defines).

There are still remaining issues - such as the 'will not remain shared '
problem. (See entry in perldiag for explaination).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding contents on yaml file without overwriting actual contents Kamarulnizam Rahim Ruby 4 01-28-2011 09:10 AM
if innerHTML used twice then it replaces the contents i want it to display all the contents virendra.amritkar@gmail.com Javascript 0 06-29-2007 08:13 AM
CGI: Execute a perl script inside another perl script xdarcos@hotmail.com Perl Misc 20 01-18-2005 12:33 PM
Execute another perl script from my perl script Petterson Mikael Perl Misc 3 01-05-2005 01:31 PM
Can I restrict both attribute contents and element contents in schema Don Adams XML 1 03-05-2004 12:48 PM



Advertisments