![]() |
What's more efficient, hash or array
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 |
Re: What's more efficient, hash or array
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 * Tong * <sun_tong@users.sourceforge.net> wrote in news:f1fe1c3a0116d67cb96e24b0d1c412f4@news.teranew 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----- |
Re: What's more efficient, hash or array
* 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 |
Re: What's more efficient, hash or array
"* Tong *" <sun_tong@users.sourceforge.net> wrote in message
news:f1fe1c3a0116d67cb96e24b0d1c412f4@news.teranew 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. |
Re: What's more efficient, hash or array
* Tong * <sun_tong@users.sourceforge.net> 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 tadmc@augustmail.com Perl programming Fort Worth, Texas |
Re: What's more efficient, hash or array
* 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 |
| All times are GMT. The time now is 09:55 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.