Also sprach Purl Gurl:
Tassilo said:
Purl said:
Tassilo v. Parseval wrote:
Purl Gurl wrote:
Purl Gurl wrote:
Jasper wrote:
(snipped)
[ benchmark snipped ]
Actually, my benchmark does a bit more to closer reflect
a simple script. Arrays are created, variables are set,
arrays are set to null.
You don't show how long it takes to access single elements, for
instance.
That is unrelated. The point of my test is to exemplify
identical code takes longer to run when lexical scoping
is employed.
I don't understand anything of the above. What is a '"single" malloc'
which is 'flagged for reuse'? Furthermore, what are those 'private
blocks'?
My hunch is you already know and are testing my knowledge.
For lexical scoping, a single chunk of memory, reserved by
perl core, can be reused many times. That is a single "malloc"
serving memory needs multiple times.
Simple example, you can push an array into that "malloc" then
later destroy it via lexical scoping, followed by another array
being pushed into that same "malloc" memory chunk.
Another here used a very good expression of calling those
memory chunks an "arena" which is more understandable;
blocks of reserved memory for use.
Ah, that. Without having looked too closely, global (package) variables
use the same memory allocation strategy. That means that memory attached
to dynamic variables will be reused as well, provided you tell perl that
it is allowed to reuse it, which for instance happens through a
statement like this:
@array = (1 .. 10000);
...
@array = (); # the memory of the 10000 scalars reusable now
"Inherently, there are lots of exceptions, oddities and other...."
As I was trying to tell you, lexical variables being quicker than
dynamic ones is not an exception, it's the rule.
In general, use of global variables lends to a faster
running script. Lexical scoping, slower. Use of "my"
declarations within private blocks increases activity
by perl core, hence, slower script completion.
No, it doesn't. The Perl interpreter runs through fewer lines of C code
when using lexical values. The whole procedure of unwrapping the scalar
(or array or hash) from a glob isn't done. With a lexical, perl knows at
compile time which of the various C structures to use. When they are put
into the pad (which is a special AV that holds the lexicals for a given
scope), they are simply casted to an SV. This is exactly one line of C
code:
#define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
And retrieving a, say, array from this pad is just a matter of doing
AV *ary = (AV*)PL_curpad[po];
Whereas you want to tell me that retrieving a dynamic variable is less
than subscripting a C array plus a typecast? After all, you said that
lexicals increase the activity in the Perl core.
As pointed out previously, it is very possible better
use of memory may actually lend to better efficiency.
I am thinking in terms of high RAM usage creating a
greater degree of paging / swap file usage, which
is slower because of hard drive access.
It doesn't have anything to do with that. Lexicals are faster because
perl has to carry out less work. I don't understand how one could
possibly argue about that.
Of course, you are free to continue using globals and pretend that
they'll make your programs perform better. Even if they slow them down.
Tassilo