While plowing my way through Damian Conway's Object Oriented Perl, I read something to the effect that, yes, you can type variables. so rather than
my $foo = Bar::Baz->new();
a script can define
my Bar::Baz $foo = Bar::Baz->new();
and $foo's methods will get checked at compile time, rather than run time, shaving precious time off script execution.
Intrigued, I decided to benchmark. I couldn't think of anything that I was using that didn't make :
use Benchmark;
use XML::Twig;
$count=50000;
Benchmark::cmpthese($count, {
'typed' => sub {
my XML::Twig $twig=XML::Twig->new();
#$twig->parsefile( 'c:\test.xml');
},
'untyped' => sub {
my $twig=XML::Twig->new();
#$twig->parsefile( 'c:\test.xml');
},
});
This brought my memory usage to a little over 800 meg after the benchmark was done. The lesson being "When a module says 'be sure to call dispose after you're done with an object', you do it!". Although, after I corrected the benchmark subs, I still wound up short 20 MB or so after all was said and done. Very mysterious.
Oh, the results?
Benchmark: timing 50000 iterations of typed, untyped...
typed: 19 wallclock secs (17.56 usr + 0.00 sys = 17.56 CPU) @ 2847.06/s (n=50000)
untyped: 20 wallclock secs (19.12 usr + 0.00 sys = 19.12 CPU) @ 2614.38/s (n=50000)
Rate untyped typed
untyped 2614/s -- -8%
typed 2847/s 9% --
I think I need to work up a larger test case.
The lesson being "When a module says 'be sure to call dispose after you're done with an object', you do it!". Although, after I corrected the benchmark subs, I still wound up short 20 MB or so after all was said and done.
Actually if you are using XML::Twig 3.00 or above and perl 5.6.1 or above you should read the part that advises you to install WeakRef as it would remove the need to dispose