2 Qs XML(loosly) and reinventing the wheel

Curtis Poe cp at onsitetech.com
Thu Feb 14 17:48:23 CST 2002


----- Original Message -----
From: "Austin Schutz" <tex at off.org>
> I'm not sure what the performance penalty is for use strict. I
> have been known to use it for testing, then leave it off in working code.
>
> Austin

Great example you gave of having problems when not using strict.

As for performance issues of not using strict, here's some wild speculation.

For globals, perl has to go through a hash lookup (finding the variable in
the symbol table for the namespace), find the appropriate slot in the
typeglob that resides there and then get to the actual variable in question.
Normally, these variables are bound (is this the right term?) at compile
time to their actual location so Perl doesn't have to jump through all of
those hoops.  However, if one falls into the trap of using variable
variables, you lose this benefit and incur the overhead:

    $fool = 'Ovid';
    $idiot = 'fool';
    print $$idiot; # prints Ovid

Also, I understand that "local" also has this issue (since you can only use
local with globals).  Lexically scoped variables, though, are stored
directly in a "scratchpad" assigned to the respective scope.  Scratchpads
are special anonymous arrays that hold the variables directly.  There is no
intervening typeglob that the variable is stored in (thus, in the pad, $foo
and @foo have no relation to one another, unlike globals).  It's my
understanding that variable lookup in a pad is quick (are these also bound
at runtime?) and without the typeglob, my variables are faster than local
ones.

So, as far as I am aware, strict becomes a performance issue when you force
Perl to go back to a symbol table and look things up.  However, I'm of the
"don't worry about performance up front" camp, so this is the least of my
worries.  My worry is when someone accidentally does things like this:

    $foo = '[';
    $bar = 'foo';
    print $$bar; # prints [

That might seem innocent, but guess what this does:

    $$foo = 3;

If that follows the above code, you've just made the first index of all
arrays 3 instead of zero (see perldoc perlvar and lookup $[).  Try and debug
that :)

Cheers,
Curtis "Ovid" POe
=====
"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push at A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift at a;shift at a if $a[$[]eq$[;$_=join q||, at a};print $_,$/for reverse @A

TIMTOWTDI



More information about the Pdx-pm-list mailing list