antirice has asked for the wisdom of the Perl Monks concerning the following question:

I am aware that eval is pretty slow. I am using an array to store the order through which data should be passed between subs. When the path is determined, a number of values will be processed through the subs referenced by this array. What I'd prefer to do is avoid the overhead of iterating through this array and just create a temp sub that processes everything in place. I can easily create something to write this sub. What I was wondering concerns speed. Should I eval the text for the sub or should I write out to a file and require it? Please note that these steps sometimes takes up to half an hour to process in short bursts and then the script is rerun with different parameters to create a new path that the data must use. I cannot generate these paths beforehand as the paths change based upon a number of factors at runtime. I understand that eval vs require shouldn't be my biggest problem but as an academic excercise, which is faster?

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1

Replies are listed 'Best First'.
Re: require vs eval
by BrowserUk (Patriarch) on Jun 16, 2003 at 21:01 UTC

    I'm not very familiar with the internals of perl, but logic suggests that require is essentially eval where the data to be eval'd comes from a file. Therefore, the evaluation (parsing, compiling etc) of the text will take the same amount of time either way.

    The difference will be the overhead in writing the text to a file only to read it back in.

    There's more to it than that, but all of that "more to it" is additional overhead on the require, making the eval look even better.

    That said, I'd like to see the code you are evaling. 5 gets you 2 that there is a better way:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      Ah, you're absolutely right BrowserUK. Straight from perldoc -f require:

      ...Otherwise, demands that a library file be included if it hasn't already been included. The file is included via the do-FILE mechanism, which is essentially just a variety of eval...

      In other words, DOH! *slaps own hand for RTFM for eval but not for require*

      antirice    
      The first rule of Perl club is - use Perl
      The
      ith rule of Perl club is - follow rule i - 1 for i > 1

Re: require vs eval
by chromatic (Archbishop) on Jun 16, 2003 at 20:25 UTC

    Disk I/O is usually a bottleneck. eval seems like it'd be faster, but the only way to be sure is to Benchmark your particular application.

    Would it be easier still to throw around subroutine references and avoid either step altogether?

Re: require vs eval
by bobn (Chaplain) on Jun 16, 2003 at 20:39 UTC
    If the steps take half an hour, I can't help but think that the cost of iterating through an array of coderefs will be trivial.

    --Bob Niederman, http://bob-n.com