What's more efficient, hash or array

T

* Tong *

Hi,

Consider the following data structure, what's more efficient, hash or array?

$precord{name},
$precord{age},
$precord{birthday},

I used to used this form. It is clearer. Then one day I thought
that using hash should be expensive than using the direct-access
array. So I change the above hash into array:

$precord[$NDX_NAME],
$precord[$NDX_AGE],
$precord[$NDX_BIRTHDAY],

But just recently I read somewhere that hash is more efficient
that array.

So, I am really curious to know what's more efficient in the above
two approaches, and how much better one over the other. Thanks.
 
E

Eric J. Roode

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

Consider the following data structure, what's more efficient, hash or
array?

$precord{name},
$precord{age},
$precord{birthday},

I used to used this form. It is clearer. Then one day I thought
that using hash should be expensive than using the direct-access
array. So I change the above hash into array:

$precord[$NDX_NAME],
$precord[$NDX_AGE],
$precord[$NDX_BIRTHDAY],

But just recently I read somewhere that hash is more efficient
that array.

I'm curious where you read that.
So, I am really curious to know what's more efficient in the above
two approaches, and how much better one over the other. Thanks.

I don't think it matters a whole heck of a lot. You can gain a small
measure of speed by using constants for the indices instead of
variables that you set. But frankly, if speed is so important that
you're considering jumping through hoops in order to shave a little
time off, you're using the wrong language.

- --
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: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE/195aY96i4h5M0egRAtPjAJ9K4nMoeG9uF2AUr8OHK3SKY7CTXACdEz5Q
DcO/gNaIg2Us9ZkbpejMtvw=
=xf3t
-----END PGP SIGNATURE-----
 
B

Bob Walton

* Tong * said:
Hi,

Consider the following data structure, what's more efficient, hash or array?

$precord{name},
$precord{age},
$precord{birthday},

I used to used this form. It is clearer. Then one day I thought
that using hash should be expensive than using the direct-access
array. So I change the above hash into array:

$precord[$NDX_NAME],
$precord[$NDX_AGE],
$precord[$NDX_BIRTHDAY],

But just recently I read somewhere that hash is more efficient
that array.

So, I am really curious to know what's more efficient in the above
two approaches, and how much better one over the other. Thanks.


Why don't you

use Benchmark;

and find out?
 
J

Jim Keenan

* Tong * said:
Hi,

Consider the following data structure, what's more efficient, hash or array?

$precord{name},
$precord{age},
$precord{birthday},

I used to used this form. It is clearer. Then one day I thought
that using hash should be expensive than using the direct-access
array. So I change the above hash into array:

$precord[$NDX_NAME],
$precord[$NDX_AGE],
$precord[$NDX_BIRTHDAY],

But just recently I read somewhere that hash is more efficient
that array.

So, I am really curious to know what's more efficient in the above
two approaches, and how much better one over the other. Thanks.
Supply a definition of "efficient." The one I use, at its most general, is
"useful output divided by input" -- but that still requies that I define
"useful." So your measure of efficiency depends entirely on your definition
of utility.
 
T

Tad McClellan

* Tong * said:
Consider the following data structure, what's more efficient, hash or array?


use Benchmark;

with your actual data, and you can see for yourself.

$precord{name},
$precord{age},
$precord{birthday},

I used to used this form. It is clearer. Then one day I thought
that using hash should be expensive than using the direct-access ^^^^^
array. So I change the above hash into array:


It depends on what "use" your "using" is.

$precord[$NDX_NAME],
$precord[$NDX_AGE],
$precord[$NDX_BIRTHDAY],

But just recently I read somewhere that hash is more efficient
that array.


Efficient at what, exactly?

Memory usage?

Access time?

Time to look something up? (hashes kick butt here)

Time to add an entry?

Time to remove an entry?

Programming time, both development and maintenance (this costs the
most, so it is the most-often optimized one).

So, I am really curious to know what's more efficient in the above
two approaches, and how much better one over the other. Thanks.


It depends on what you are going to do with them.

What are you going to do with them?
 
J

Jürgen Exner

* Tong * said:
Consider the following data structure, what's more efficient, hash or
array?

$precord{name},
$precord{age},
$precord{birthday},

I used to used this form. It is clearer. Then one day I thought
that using hash should be expensive than using the direct-access
array. So I change the above hash into array:

$precord[$NDX_NAME],
$precord[$NDX_AGE],
$precord[$NDX_BIRTHDAY],

But just recently I read somewhere that hash is more efficient
that array.

A program using the hash is much more efficient to program and much more
efficient to maintain.
An array whose values types are all over the place (what does a name have in
common with an age?) is just a nightmare to understand and to use.
Just imagine: would it make sense to sort that array? Would it make sense to
compute the average? Would any of the typical array operations make sense?
If not, then probably an array is not the right data structure.

Oh, you didn't mean efficiency in programming and maintance but efficiency
at runtime? Well, for maybe 99.9% of all applications runtime efficiency is
really no issue. Those programs are waiting for I/O, for human interaction,
are run as batch processes, or, or, or, ....
Ease of programming and in particular maintenance is far more important and
valuable.

If indeed you are in a situation where you are doing something very
time-consuming like maybe DNA analysis or fluid dynamics or astronomical
simulations then the first step is always to optimize your algorithm. That
is where you can make a real difference.
And then if that is not sufficient, switch to a different programming
language, e.g. C or even assembler for the most time-critical parts.
But performance gains from using an array over a hash or vice versa are
really neglegable and often not even noticable.

jue
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top