|Problems? Is your data what you think it is?|
use vs. require in string eval?by lachoy (Parson)
|on Mar 11, 2004 at 21:50 UTC||Need Help??|
lachoy has asked for the wisdom of the Perl Monks concerning the following question:
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:
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:
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:
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...