Script Execution time and Use

B

Bigus

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
 
P

Peter Makholm

Bigus said:
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.
 
B

Bigus

Peter Makholm said:
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?
 
T

Teo

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
 
G

Gunnar Hjalmarsson

Bigus said:
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.
 
B

Bigus

Teo said:
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
 
B

Bigus

Gunnar Hjalmarsson said:
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
 
J

Jürgen Exner

Bigus said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top