What's more efficient, hash or array

Discussion in 'Perl Misc' started by * Tong *, Dec 11, 2003.

  1. * Tong *

    * Tong * Guest

    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
    * Tong *, Dec 11, 2003
    #1
    1. Advertising

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

    * Tong * <> wrote in
    news::

    > 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-----
    Eric J. Roode, Dec 11, 2003
    #2
    1. Advertising

  3. * Tong *

    Bob Walton Guest

    * 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
    Bob Walton, Dec 11, 2003
    #3
  4. * Tong *

    Jim Keenan Guest

    "* Tong *" <> wrote in message
    news:...
    > 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.
    Jim Keenan, Dec 11, 2003
    #4
  5. * Tong * <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 11, 2003
    #5
  6. * 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
    Jürgen Exner, Dec 11, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    478
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    255
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    212
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    581
    David A. Black
    Jul 2, 2008
  5. Intransition
    Replies:
    5
    Views:
    126
    Intransition
    May 5, 2010
Loading...

Share This Page