When XML::Twig Attacks !

boo_radley on 2002-02-04T16:47:55

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.


Ain't no faster

autarch on 2002-02-04T17:28:01

'Typing' your variables will not get you a speed increase unless the variable being typed is a pseudo-hash object and you're accessing the hash keys directly.

IOW, don't bother.

-dave

Reading lesson, part 2

mirod on 2002-02-04T17:33:11

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 ;--)