I would suppose because he measured it using some method that doesn't
depend on the programming language, but rather on the operating system.
You mean that this isn't one script but two and he measured the complete
run-time of each? Possible, but we can't know, so I asked. It is
important to know *what* he measured - otherwise we cannot know what is
slow and cannot answer the question.
Unless, of course, that isn't what was being measured. The OP didn't
say that one retrieval was shorter than another, but rather that our was
faster than my.
Actually, he labelled his two snippets "Fast key rettieve" and "Slow key
rettieve", and I assume that "rettieve" was supposed to read "retrieve".
Which, by the way, matches my experiences. There is something
pathological about the garbage collection of very large "my" hashes.
I don't think it's particularly pathological: If a lexical variable goes
out of scope, it needs to be destroyed, which means freeing each of its
elements. For a few million elements, that takes some time.
A global variable never goes out of scope, so you don't have to do that
(but you still have to examine each element to check if it has a DESTROY
method).
And note that
doesn't print a newline. So it won't get flushed until the program ends
which will be after garbage collection. So to the casual observer it
looks as if retrieving $hash{153} takes two seconds while it really
takes probably a microsecond or so.
hp