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

Hi. I have a question about using modules. Do modules have the tendency to slow down programs? Or are they efficient in that way. Or does that depend on individual cicumstances?

Replies are listed 'Best First'.
Re: using modules
by tobyink (Canon) on Jul 18, 2013 at 07:39 UTC

    All other things being equal; a lot of code will probably be slower than a little bit of code. Perl needs to load the code off the disk, and compile it into its in-memory representation (the so called "op tree") - and more code takes longer to load and compile.

    However a fast algorithm that uses a lot of code will run faster than a slow algorithm written in a small amount of code. And for most non-trivial scripts, run time is more of a significant factor than compile time. A good module written by someone who knows about the topic at hand, and is aware of optimization techniques may well run faster than the code you'd write yourself.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: using modules
by hdb (Monsignor) on Jul 18, 2013 at 07:15 UTC

    Do you mean re-factoring your own code into modules or using modules from CPAN? For the former there is probably very little overhead if done properly.

    As for the latter, it depends. Modules on CPAN are often provided and maintained by very experienced programmers, so they are likely to be better than your own code. Unless your are THE expert in the field that you want to program for or you need something really special. Modules on CPAN often aim at general applicability which might create some overhead.

Re: using modules
by dsheroh (Monsignor) on Jul 18, 2013 at 08:43 UTC
    No, being in a module vs. being in the main program has virtually zero effect on how fast or slow any given chunk of code will run. ("Virtually" zero because having to open an additional file may add a millisecond or two to the program's startup time.)

    An individual module may slow down your program considerably, but that's because it's doing something that's slow to do. Being in a module has nothing to do with that. Putting the same code into the main program instead of a module will still be just as slow.

Re: using modules
by Anonymous Monk on Jul 18, 2013 at 07:00 UTC

    Hi. I have a question about using modules.

    Doesn't look that way to me :)

    Do modules have the tendency to slow down programs?

    No, of course not; at least not any more than "programs" have a tendency to slow down "themselves"

    Or are they efficient in that way.

    What is "that way" and what does "efficient" mean (*)?

    Or does that depend on individual cicumstances?

    No, it depends on algorithms and circumstances (hardware)

    Do you know what is a module? ( * * )

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: using modules
by Anonymous Monk on Jul 19, 2013 at 04:35 UTC

    Perhaps i misunderstood the situation.