Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > What's more efficient, hash or array

Reply
Thread Tools

What's more efficient, hash or array

 
 
* Tong *
Guest
Posts: n/a
 
      12-11-2003
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.


--
Tong (remove underscore(s) to reply)
*niX Power Tools Project: http://xpt.sourceforge.net/
- All free contribution & collection
 
Reply With Quote
 
 
 
 
Eric J. Roode
Guest
Posts: n/a
 
      12-11-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

* Tong * <(E-Mail Removed)> wrote in
news:(E-Mail Removed) s.com:

> 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/195aY96i4h5M0egRAtPjAJ9K4nMoeG9uF2AUr8OHK3SKY7CTXA CdEz5Q
DcO/gNaIg2Us9ZkbpejMtvw=
=xf3t
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      12-11-2003
* Tong * wrote:

> 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?

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
Jim Keenan
Guest
Posts: n/a
 
      12-11-2003
"* Tong *" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) s.com...
> 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.


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-11-2003
* Tong * <(E-Mail Removed)> wrote:

> 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?


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-11-2003
* Tong * wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Sort by hash vaule, an array of hash references fahdsultan@gmail.com Perl Misc 11 10-10-2005 09:35 PM



Advertisments