Forum Moderators: coopster

Message Too Old, No Replies

Which is the fastest

int key, string key or define?

         

tata668

4:48 am on Feb 16, 2005 (gmt 0)

10+ Year Member



Which is the fastest when finding a value associated to a key?

$myArray[123] = "my value" ;
$val = $myArray[123] ;

$myArray["123"] = "my value" ;
$val = $myArray["123"] ;

define("C123", "my value") ;
$val = C123 ;

Is using a integer key increase performance while retrieving a value?

When it's possible, is using "define()" faster?

Are they all the same?

hakre

12:28 pm on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



quite an interesting question. the docs say
There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices.

so i guess for php looking up a value from an array will make no difference which kind of key is used; maybe only a very small one, because 8 does not need as much memory as "eight". less memory = more speed, but this shouldn't be the blast.

anyway, i haven't found a speed comparison chart about that and it could be cool if someone will run a test on that ;).

Addition:

Only scalar data (boolean, integer, float and string) can be contained in constants.
so define is out of range for your speed comparison.

tata668

2:44 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices

I doubt this means both will perform equally. It may only means that both use the reserved word "array"...

I think a integer key could potentially be faster than a string key because it may then be used internally as a "real" array.. Compilator (or "interpreter", whatever) could jump immediately to the given index. Using a associative array, with a string key, the compilator would have to hash the key first to find the associated value. And as you say, the longer the string is, the longer it'll take. I guess.

But hey, that's just hypothesis.

so define is out of range for your speed comparison.

Well.. In my particual case I only need to store strings so I guess define could also be used.

What I'm currently developing is a multilingual application (by the way sorry if my english is not perfect). Depending of the language the user chooses, an appropriated include is done.
And I wonder how to include the languages specific strings in the fastest way.

But you're right: I should do some benchmarks testing...

But I wanted to ask you guys first! ;-)
Thanks for your help.

tata668

2:53 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



Damn! I just think about this:

The compilator can't use a integer key to jump to a specific index because this kind of "array" is valid:

myArray[1] = "aaa" ;
myArray[4] = "bbb" ;

The indexes are not necessarily contiguous.

I guess this answers my question...

hakre

3:48 pm on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hehe, it's so nice to quote it now a third time:
there is only one array type, which can both contain integer and string indices

and that's what i meant. but benchmarks testing will clear this out.