Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Script Execution time and Use

Reply
Thread Tools

Script Execution time and Use

 
 
Bigus
Guest
Posts: n/a
 
      04-01-2009
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


 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      04-01-2009
"Bigus" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Bigus
Guest
Posts: n/a
 
      04-01-2009

"Peter Makholm" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Bigus" <(E-Mail Removed)> 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?


 
Reply With Quote
 
Teo
Guest
Posts: n/a
 
      04-01-2009
On Apr 1, 5:39*pm, "Bigus" <(E-Mail Removed)> wrote:
> "Peter Makholm" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > "Bigus" <(E-Mail Removed)> 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-01-2009
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
 
Reply With Quote
 
Bigus
Guest
Posts: n/a
 
      04-01-2009
"Teo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> 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


 
Reply With Quote
 
Bigus
Guest
Posts: n/a
 
      04-01-2009

"Gunnar Hjalmarsson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-01-2009
"Bigus" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Block execution of script while same script is already running MrBanabas@googlemail.com Ruby 2 12-17-2008 06:03 PM
Get cgi script to begin execution of another script... sophie_newbie Python 1 03-13-2008 02:52 PM
private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack sndive@gmail.com Python 9 11-14-2007 10:31 PM
Script Execution Time =?Utf-8?B?R3VydQ==?= ASP .Net 0 06-22-2006 07:00 AM
How to get asp execution time and database connection time ? Kelvin ASP General 1 07-13-2005 11:36 AM



Advertisments