hash key

G

George Mpouras

Can anyone explain why 'my' is slower than 'our' ?

# Fast key rettieve
our %hash;
for (1 .. 5_000_000) {$hash{$_}=1}
print "filled\n";
print $hash{153};

# Slow key rettieve
my %hash;
for (1 .. 5_000_000) {$hash{$_}=1}
print "filled\n";
print $hash{153};
 
P

Peter J. Holzer

Can anyone explain why 'my' is slower than 'our' ?

# Fast key rettieve
our %hash;
for (1 .. 5_000_000) {$hash{$_}=1}
print "filled\n";
print $hash{153};

# Slow key rettieve
my %hash;
for (1 .. 5_000_000) {$hash{$_}=1}
print "filled\n";
print $hash{153};

This script doesn't contain any code to measure time. How do you know
that the first part is faster? (I also see only to "retrievals" in this
code ($hash{153}), and any attempt to measure the time of a single hash
access is almost certainly futile)

hp
 
X

Xho Jingleheimerschmidt

Peter said:
This script doesn't contain any code to measure time. How do you know
that the first part is faster?

I would suppose because he measured it using some method that doesn't
depend on the programming language, but rather on the operating system.

(I also see only to "retrievals" in this
code ($hash{153}), and any attempt to measure the time of a single hash
access is almost certainly futile)

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.

Which, by the way, matches my experiences. There is something
pathological about the garbage collection of very large "my" hashes.

Xho
 
P

Peter J. Holzer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,213
Messages
2,571,105
Members
47,698
Latest member
TerraT521

Latest Threads

Top