andreychek recently emailed me with some very interesting profiling results from Class::Factory. It's a simple module that doesn't do very much, but one of the things it does do is bring in classes dynamically. So you can do something like:

$my_factory->register_factory_type( my_type => 'My::Impl::Class' );

And 'My::Impl::Class' doesn't actually get brought in until it's used for the first time. Easy enough.

The problem Eric found is in this statement that brings in the module:

eval "require $object_class";

Simple enough, right? Apparently not. According to the profiling he did that procedure took 4.5% of his program's execution time. Unacceptable for something so simple.

Here's the interesting part: when he changed it to:

eval "use $object_class";

the time took by the procedure dropped tenfold, to 0.44%.

I have no idea why this would happen. I was always under the impression that 'require' actually did less than 'use' since it doesn't invoke 'import()' automatically. But then it takes less time to work. So what's the scoop?

NB: I haven't looked at the code Eric is profiling, and it might be something as simple as the filesystem caching the modules so on successive runs they take less time to load. But then again...

Chris
M-x auto-bs-mode


In reply to use vs. require in string eval? by lachoy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.