http://qs321.pair.com?node_id=172819

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

I have about ten routines that are common to a number of classes. These routines are kept in a package called Func. After profiling an application with DProf, I was surprised to find that most of the time is being spent in the BEGIN section of Func. Here is the output from the profiler:
Total Elapsed Time = -36.6621 Seconds User+System Time = 86.44528 Seconds Exclusive Times %Time ExclSec CumulS #Calls sec/call Csec/c Name 28.4 24.62 28.883 738874 0.0000 0.0000 SE::Util::Func::BEGIN
Here is the top of the Func module:
package SE::Util::Func; use Exporter; @ISA = ('Exporter'); @EXPORT= qw( SE_cat SE_dump SE_getName SE_getValue SE_isTag SE_matchN +ame SE_matchNamePattern SE_prog SE_trim ); require 5.6.1; use strict;
Why is perl spending so much time in the BEGIN section of Func? Thanks.

Replies are listed 'Best First'.
Re: Why is perl spending so much time in the BEGIN section?
by chromatic (Archbishop) on Jun 08, 2002 at 21:50 UTC
    Exporter's a bit heavy compared to small programs. Why does that show up in BEGIN blocks in your code? Well...
    $ bleadperl -MO=Deparse package SE::Util::Func; use Exporter; @ISA = ('Exporter'); @EXPORT= qw( SE_cat SE_dump SE_getName SE_getValue SE_isTag ); require 5.6.1; use strict;
    produces:
    package SE::Util::Func; @ISA = 'Exporter'; @EXPORT = ('SE_cat', 'SE_dump', 'SE_getName', 'SE_getValue', 'SE_isTag +'); require 5.006001; sub SE::Util::Func::BEGIN { package SE::Util::Func; require Exporter; do { 'Exporter'->import }; } sub SE::Util::Func::BEGIN { package SE::Util::Func; require strict; do { 'strict'->import }; }
Re: Why is perl spending so much time in the BEGIN section?
by Anonymous Monk on Jun 08, 2002 at 20:43 UTC
    I don't know because your slow code could be many places inside of the module. It could be pretty much anything you are doing when you just execute the module, opening a database connection, initializing variables, etc.

    Just try to use SE::Util::Func and see how slow that is, then put print statements in the module to see where it gets to in a hurry, and where it slows down.