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

I'm just playing with benchmarking,
and realised that require costs
time at runtime, while use does not.

Itis obvious, because use is loaded
in the BEGIN block, and require later, so my
benchmarking does not see it.

Now the question: is use faster ?
(meaning: compile-time AND run-time )
or are they both equal ?

p.s. i am using require to load some
modules on demand (using if-blocks),
that are needed only 10% of the times
the script runs, so it will not have to compile
everything everytime - but maybe i am not right in that ?

so there is a second little question:

if (1 == 2) { require "Foo.pl"; }
is Foo.pl loaded and compiled here ?

Replies are listed 'Best First'.
Re: what's faster: use or require
by Mr. Muskrat (Canon) on Dec 07, 2002 at 18:13 UTC

    From Perlfunc (slightly paraphrased): use Module; is exactly equivalent to BEGIN { require Module; import Module LIST; } except that Module must be a bareword. Also use Module (); is exactly equivalent to BEGIN { require Module }.

    So just do the checks inside a BEGIN block and you shouldn't notice a difference.

    In answer to your second question, I give you:

    #!/usr/bin/perl use strict; use warnings; if (1 == 2) { require "foo.pl"; } print "bar\n";
    I don't have a foo.pl in my @INC path and this caused no errors or warnings, so I would say "no".

Re: what's faster: use or require
by chromatic (Archbishop) on Dec 07, 2002 at 20:04 UTC

    I don't generally answer "what's faster?" questions, because my answer is generally "it depends" or "who cares?". There are better ways to improve startup time.

    However, I sometimes do use require instead of use to avoid loading modules until they're really needed to save memory and time. In the Perl core, you'll often see the idiom of loading Carp before croak()ing.

    As for your second question, use B::Deparse and marvel that the optimizer removes code branches that can never be reached!

Re: what's faster: use or require
by pg (Canon) on Dec 07, 2002 at 18:47 UTC
    More things to try out: (in all the examples, foo.pl does not exist)
    • if (1 == 2) { use foo; }
      This causes error right after you start your program. Because use forced a require in the BEGIN block, which is executed before the if checking.
    • if ($a == 2) { require "foo.pl"; }
      You will get an error, but it is about the uninitialized variable $a, not whether foo.pl exists. So things happen in the visually expected sequence.
    • while (1) {} require foo.pl;
      This will cause a warning about the require syntax, but not the existance of foo.pl. That happens right after you start the program, as the first thing Perl will do, is checking syntax, but not whether the module exists (next example shows this more clearly)
    • while (1) {} require "foo.pl";
      This time, the syntax error is removed. It does not cause any error saying the module is not there, which means Perl will only try to load foo.pl at the time when it is really needed, which will never happen, because of the deadloop before the require.
Re: what's faster: use or require
by dws (Chancellor) on Dec 07, 2002 at 18:51 UTC
    I'm just playing with benchmarking, and realised that require costs time at runtime, while use does not.

    You appear to be confusing "runtime" with "execution phase". Using the standard definition of runtime (i.e., script start to script exit), both use and require are going to incur costs. You do have a choice in when to incur those costs.

Re: what's faster: use or require
by Aristotle (Chancellor) on Dec 08, 2002 at 12:16 UTC
    if (1 == 2) {
    Ugh. Please revise your understanding of booleans and write if(0) { next time.

    Makeshifts last the longest.

      Not so. I have 1500+ proofs that show (1 == 2) could return true.

      And of course there's always overload if you reject the proofs ;-).

        *g* :-) thank you all,
        the answers helped me
        to find the right direction

        and of course the right meaning of
        "it depends" :-)