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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |