performance arrays vs hashes

T

Thomas

hi,

i dont know enough of perl to describe,
using hashes is better than using arrays...(or isnt so ?)
i dont know how to ask some 'google' machine for this kind of question,
but i hope we have some "perl guru's" here, whose like to answer

best regards
Thomas
 
T

Tore Aursand

using hashes is better than using arrays...(or isnt so ?)

Can't compare to totally different data types. You should use hashes when
using hashes are appropriate, and vice versa.
 
J

Jürgen Exner

Thomas said:
i dont know enough of perl to describe,
using hashes is better than using arrays...(or isnt so ?)

I don't know enough about DoItYourself to describe,
using a hammer is better than using a screw driver ... (or isnt so?)

Well, it all depends on what you want to do with it...

jue
 
W

Wolfgang Fischer

(e-mail address removed) (Thomas) wrote in



You can't really say that using hashes is better than using arrays. It
depends on what you want to do. What *do* you want to do?


Basically, using hashes is better when a hash is what you need. Using an
array is better when an array is what you need. That's all there's to it.
Read about both in


perldoc perlvar

perldoc perlvar describes the predefined variables. perldoc perldata
describes the different variable types.
 
T

Tedd Hansen

Sometimes this is true, sometimes it is not...
As the other postings said, comparing two datatypes isn't that easy. All for
it's use.

But that aside;
Often when you are working with large amounts of elements and don't know
their exact position in the array you need to find them. This often includes
searching them one by one. In this case if you where using hashes you'd find
them right away (search algorithms that uses WAY less CPU than searching the
elements one by one). (There are other advantages as well, the most obvious;
hashes are (key1->data1,key2->data2) while arrays are (data1, data2))

Hovewer, using hashes you loose the order they where added. Hashes rely on
putting the data "where it fits", for fast retrival. (Kind of like filing
"Abraham" under "A") So hashes is useless if you depend on a certain order
in things, unless you use the hash data.

There are many other things to consider, it's probably best if you read up
on perl datatypes... :)
http://www.perldoc.com/perl5.6/pod/perldata.html

- Tedd
 
J

Joe Minicozzi

What the others have said about arrays and hashes being apples and oranges
is true, but here's one way to think of it: A Perl array is like arrays in
most languages -- it's a collection of data indexed by a number. A Perl
hash can be thought of as an array, but it's not indexed by a number, but by
a string. So, to look up something in an array, you can say "$array[23]"
but you can't say "$array['abcd']. (Actually, you can but you'll get
$array[0].) You _can_ say "$hash{'abcd'}" or "$hash{'whatever I damn well
please'}" and actually, you don't need the quotes; "$hash{abcd}" works just
as well. You can even say "$hash{99}" but the 99 will be interpreted as a
string so it's the same as "$hash{'99'}.

So now you might ask, "Well, if I can index a hash like I can index an
array, why should I use an array?" There are two reasons: maintaining
element order, and performance. As some of the others said, a hash is
implemented using a hashing algorithm on the index (which in Perl, we call a
key), and this results in the elements being stored in a seemingly random
order. So if you create a hash with keys from 'A' to 'J' and the read the
hash back using something sequential like a foreach loop, you won't get them
back in 'A' to 'J' order. If you create an array with indexes from 1 to 10
and read it back sequentially, you get them back in 1 to 10 order. This
property of arrays is very useful when using the array as a stack or a
queue, where, if you add an element at the end, it has to stay at the end.
Perl's push, pop, shift and unshift functions manipulate arrays in just this
way.

The other reason is performance. Although it is somewhat variable, I've
seen estimates that it is about 20% faster to use an array instead of a
hash. But don't let that sway you too much. If you need data to be indexed
using a string, then a hash is just what you want. But if you can
conveniently use a number as the index, an array is a better choice. And if
you want a stack or a queue or a plain list, an array is really the only
choice.

Hope this helps. You might also look up 'hashing' or 'hash algorithm' in
Google to get a better understanding of how a hash works.
 
T

Tad McClellan

Joe Minicozzi said:
You _can_ say "$hash{'abcd'}" or "$hash{'whatever I damn well
please'}" and actually, you don't need the quotes;


You don't need the quotes when the key matches /^\w+$/.

So you need the quotes for something like that 2nd one above.
 
J

Joe Minicozzi

Tad McClellan said:
You don't need the quotes when the key matches /^\w+$/.

So you need the quotes for something like that 2nd one above.

Oops. Thanks for pointing that out. I did RTFM, but I didn't CRTFM
(Carefully ...).
 
T

Tore Aursand

You don't need the quotes when the key matches /^\w+$/.
So you need the quotes for something like that 2nd one above.

Word of advice, but shouldn't one use quotes whenever possible? I
constantly use quotes, as it looks better in my editor (FTE).

I don't use quotes when working with variables, of course;

my $value = $hash{ $key };

Hmm. But I tend to carry around those spaces, I see. :) Easier to read?
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Word of advice, but shouldn't one use quotes whenever possible? I
constantly use quotes, as it looks better in my editor (FTE).

I don't use quotes when working with variables, of course;

my $value = $hash{ $key };

Hmm. But I tend to carry around those spaces, I see. :) Easier to
read?

It's a matter of taste. I prefer omitting the quotes, because to me,

$hash{foo}

is analogous to

$array[7]

and you don't write $array['7'], do you? (even though you could, in
Perl).

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP5e4gmPeouIeTNHoEQJUVgCfRdf4LiJdpHhjQggWURjQZErUDyoAoNlG
pUCI/8+2z1bi4oC8yjd5MA8O
=ocVj
-----END PGP SIGNATURE-----
 
B

Bart Lateur

Thomas said:
i dont know enough of perl to describe,
using hashes is better than using arrays...(or isnt so ?)

From your description of yourself I'd think you don't have enough
experience in perl to even worry about this.

Natively, hash access and array access are quite comparable in speed, I
think hash access could be to 30% slower than array access. But anything
you can do to replace hashes by arrays, when hashes are the best tool
for the job, is bound to be *much slower* than just using a hash.

Don't worry about it. Use the data structure that's best for the job at
hand.
 
T

Tore Aursand

It's a matter of taste. I prefer omitting the quotes, because to me,

$hash{foo}

is analogous to

$array[7]

and you don't write $array['7'], do you?

No, but that's because numbers _and_ strings are highlighted in my editor,
so I assume everything boils down to the editor;

$hash{ 'foo' }; # highlighted
$array[ 7 ]; # highlighted (as number)
$array[ '7' ]; # highlighted (as string)
$hash{ foo }; # not highlighted

I'm referring to the value inside {} and [] when talking about
highlighted, of course.

Anyway: Nothing to do with Perl. Move on. Nothing to see here. :)
 

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

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top