Script Execution time and Use

Discussion in 'Perl Misc' started by Bigus, Apr 1, 2009.

  1. Bigus

    Bigus Guest

    Hi

    I have a load of scripts that use various different cpan modules and I
    thought to tidy up the header of each script I'd try bundling all the "use
    ..." commands into one init file in my modules folder. So in each script, at
    the top, I have:

    use strict;
    use Modules::Init;

    In Init.pm there are about 30 "use ..." lines. Using a basic test script
    (test.pl) I enclosed the two lines above with a timer, ie:

    use Devel::Timer;
    my $t = Devel::Timer->new();
    $t->mark("inits");
    use strict;
    use Modules::Init;
    $t->mark("done init");
    print $t->report();

    That tells me that it took about 10 microseconds to run, which would be
    great, however if I time the test script from the linux command line using:

    time perl test.pl

    it tells me that the script took about 1.6 seconds.

    Evidently, the idea of use'ing all the modules in one place isn't going to
    be a good one as it'll slow things down too much, but what I don't
    understand, and was wondering if someone could clarify for me, is why there
    is such a big difference in the two methods of timing?

    Thanks
    Bigus
     
    Bigus, Apr 1, 2009
    #1
    1. Advertising

  2. "Bigus" <> writes:

    > use Devel::Timer;
    > my $t = Devel::Timer->new();
    > $t->mark("inits");
    > use strict;
    > use Modules::Init;
    > $t->mark("done init");
    > print $t->report();


    use-statements happens at compile time, so you really havn't any
    operations between you two marks.
     
    Peter Makholm, Apr 1, 2009
    #2
    1. Advertising

  3. Bigus

    Bigus Guest

    "Peter Makholm" <> wrote in message
    news:...
    > "Bigus" <> writes:
    >
    >> use Devel::Timer;
    >> my $t = Devel::Timer->new();
    >> $t->mark("inits");
    >> use strict;
    >> use Modules::Init;
    >> $t->mark("done init");
    >> print $t->report();

    >
    > use-statements happens at compile time, so you really havn't any
    > operations between you two marks.


    I'm not sure I understand. I'm probably being thick but I thought Perl did
    the compilation first, so that it can check for syntax errors etc, and then
    subsequently runs the script which is when the timer command would be
    reporting at?
     
    Bigus, Apr 1, 2009
    #3
  4. Bigus

    Teo Guest

    On Apr 1, 5:39 pm, "Bigus" <> wrote:
    > "Peter Makholm" <> wrote in message
    >
    > news:...
    >
    > > "Bigus" <> writes:

    >
    > >> use Devel::Timer;
    > >> my $t = Devel::Timer->new();
    > >> $t->mark("inits");
    > >> use strict;
    > >> use Modules::Init;
    > >> $t->mark("done init");
    > >> print $t->report();

    >
    > > use-statements happens at compile time, so you really havn't any
    > > operations between you two marks.

    >
    > I'm not sure I understand. I'm probably being thick but I thought Perl did
    > the compilation first, so that it can check for syntax errors etc, and then
    > subsequently runs the script which is when the timer command would be
    > reporting at?


    Exacly: and when it compiles it also loads the modules specified by
    'use'. Which means that when you execute the code the modules are
    already loaded.

    Matteo
     
    Teo, Apr 1, 2009
    #4
  5. Bigus wrote:
    >
    > In Init.pm there are about 30 "use ..." lines. Using a basic test script
    > (test.pl) I enclosed the two lines above with a timer, ie:
    >
    > use Devel::Timer;
    > my $t = Devel::Timer->new();
    > $t->mark("inits");
    > use strict;
    > use Modules::Init;
    > $t->mark("done init");
    > print $t->report();
    >
    > That tells me that it took about 10 microseconds to run, which would be
    > great,


    Exchange the line

    use Modules::Init;

    for

    require Modules::Init;

    and Devel::Timer will probably report a significantly longer time.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 1, 2009
    #5
  6. Bigus

    Bigus Guest

    "Teo" <> wrote in message
    news:...
    >> I'm not sure I understand. I'm probably being thick but I thought Perl
    >> did
    >> the compilation first, so that it can check for syntax errors etc, and
    >> then
    >> subsequently runs the script which is when the timer command would be
    >> reporting at?

    >
    > Exacly: and when it compiles it also loads the modules specified by
    > 'use'. Which means that when you execute the code the modules are
    > already loaded.


    Oh I see, so it's already loaded the modules before it gets to running the
    script and thus the timer.. got it.. phew :)

    Thanks
    Bigus
     
    Bigus, Apr 1, 2009
    #6
  7. Bigus

    Bigus Guest

    "Gunnar Hjalmarsson" <> wrote in message
    news:...
    > Bigus wrote:
    >>
    >> In Init.pm there are about 30 "use ..." lines. Using a basic test script
    >> (test.pl) I enclosed the two lines above with a timer, ie:
    >>
    >> use Devel::Timer;
    >> my $t = Devel::Timer->new();
    >> $t->mark("inits");
    >> use strict;
    >> use Modules::Init;
    >> $t->mark("done init");
    >> print $t->report();
    >>
    >> That tells me that it took about 10 microseconds to run, which would be
    >> great,

    >
    > Exchange the line
    >
    > use Modules::Init;
    >
    > for
    >
    > require Modules::Init;
    >
    > and Devel::Timer will probably report a significantly longer time.


    Yes, you are quite right. So, 'require' loads the modules at run time,
    whereas 'use' loads them at compile time.

    Thanks
    Bigus
     
    Bigus, Apr 1, 2009
    #7
  8. "Bigus" <> wrote:
    >Yes, you are quite right. So, 'require' loads the modules at run time,
    >whereas 'use' loads them at compile time.


    Why don't you simply check it out?

    perldoc -f use


    jue
     
    Jürgen Exner, Apr 1, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?R3VydQ==?=

    Script Execution Time

    =?Utf-8?B?R3VydQ==?=, Jun 22, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    351
    =?Utf-8?B?R3VydQ==?=
    Jun 22, 2006
  2. flamesrock
    Replies:
    8
    Views:
    548
    Hendrik van Rooyen
    Nov 24, 2006
  3. Replies:
    9
    Views:
    401
    Michael DOUBEZ
    Feb 5, 2009
  4. Dave

    Script Execution Time

    Dave, Jul 20, 2004, in forum: ASP General
    Replies:
    6
    Views:
    123
  5. Kelvin
    Replies:
    1
    Views:
    221
    Aaron Bertrand [SQL Server MVP]
    Jul 13, 2005
Loading...

Share This Page