What is RSS?
serial array vs. hash....
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > Java Programming > serial array vs. hash.... 10 January 2002 16:21:19

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

serial array vs. hash....

Eric McKeown 10 January 2002 16:21:19
 Hi folks,

Quick question about the performance implications of looking up info in
a hash (associative array) vs. looking it up in a serial array. I have
a script that will be running a function (actually several similar
functions) up to 1500 times to verify that certain data values are in a
valid list. My list might have anywhere from 2 to 100 valid values. I
could store the list in a conventional serial array...

$valid[0] = "Valid One";
$valid[1] = "Valid Two";
$valid[2] = "Valid Three";
.
and so on, and then use a for loop to iterate through the contents of
the array each time that I need to check to see if I have a valid
value. On the other hand, I could create a hash that looks like...

$valid["Valid One"] = "some value";
$valid["Valid Two"] = "some other value";

and so on, and then simply check to see if the entry exists in the
associative array in order to determine whether it is valid. I suspect
that the latter method would be much more efficient, and I intend to
test the two techniques myself to find out, but I just thought I would
check here to see if anyone could confirm or refute my hunch....

Thanks for your help,

Eric

Add comment
Tim Ward 9 January 2002 21:17:01 permanent link ]
 As far as I know all arrays in php are hash arrays. Your "serial" array is
actually a hash array with numeric keys, so in your example using numeric
keys and having to loop through them has nothing going for it.

I'm sure someone will tell me if I'm wrong.

Tim
www.chessish.com <http://www.chessis­h.com>

----------
From: Eric McKeown [SMTP:ericm@palaver.net]
Sent: 09 January 2002 05:53
To: php-general@lists.p­hp.net
Subject: serial array vs. hash....

Hi folks,

Quick question about the performance implications of looking up info
in
a hash (associative array) vs. looking it up in a serial array. I
have
a script that will be running a function (actually several similar
functions) up to 1500 times to verify that certain data values are
in a
valid list. My list might have anywhere from 2 to 100 valid values.
I
could store the list in a conventional serial array...

$valid[0] = "Valid One";
$valid[1] = "Valid Two";
$valid[2] = "Valid Three";
.
and so on, and then use a for loop to iterate through the contents
of
the array each time that I need to check to see if I have a valid
value. On the other hand, I could create a hash that looks like...

$valid["Valid One"] = "some value";
$valid["Valid Two"] = "some other value";

and so on, and then simply check to see if the entry exists in the
associative array in order to determine whether it is valid. I
suspect
that the latter method would be much more efficient, and I intend to
test the two techniques myself to find out, but I just thought I
would
check here to see if anyone could confirm or refute my hunch....

Thanks for your help,

Eric

Add comment
Mike Ford 10 January 2002 16:21:19 permanent link ]
 
-----Original Message-----> From: Eric McKeown [mailto:ericm@palaver.net]> Sent: 09 January 2002 05:53>
could store the list in a conventional serial array...>
$valid[0] = "Valid One";> $valid[1] = "Valid Two";> $valid[2] = "Valid Three";> .> and so on, and then use a for loop to iterate through the contents of> the array each time that I need to check to see if I have a valid> value.

Well, for a kick-off I'd use the built-in in_array() function rather than writing a for loop.
On the other hand, I could create a hash that looks like...>
$valid["Valid One"] = "some value";> $valid["Valid Two"] = "some other value";>
and so on, and then simply check to see if the entry exists in the> associative array in order to determine whether it is valid. > I suspect> that the latter method would be much more efficient

My suspicions would be the same as yours (but I haven't tested it). If your "some value", "some other value", etc. are actually values that you are going to use, then this is clearly the right solution. If you're just looking the values up, I might tend to the plain array solution simply for readability/underst­andability -- especially if the difference between in_array() and an associative lookup is small.

(BTW -- and pardon me if you already know this, but it's not clear from your query! -- if you do use the associative array method as a pure lookup, there's no need to assign different values for each index -- indeed, I'd probably go for something like this:

$valid["Valid One"] = TRUE;
$valid["Valid Two"] = TRUE;

as being the most obvious representation of your intentions!)

Cheers!

Mike

-------------------­--------------------­--------------------­----------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > Java Programming > serial array vs. hash.... 10 January 2002 16:21:19

see also:
Title bar question
Is there anyway to work on the…
Creating a unique id based on a file…
пройди тесты:
What is Your Temperament?
see also:
THIS IS ONE MAIL THAT BRINGS TEARS TO…
LOVE KNOWS NO BOUNDS-I SACRIFICED…
LIFE TOOK ME A LITTLE FAR AWAY. MY ONLY…

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .