08.19
I was wondering whether there is a performance difference between count() and sizeof().
Test code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <?PHP // CREATE TEST ARRAY $a = array('a','b','c','d','e','f','g','h','i','j'); // START TIMER ONE $TimerOne = benchmarkTimerStart(); // CALL COUNT FUNCTION 1000000 TIMES for($i=0;$i<1000000;$i++) { count($a); } // STOP TIMER ONE $ResultOne = benchmarkTimerStop($TimerOne); // START TIMER TWO $TimerTwo = benchmarkTimerStart(); // CALL SIZEOF FUNCTION 1000000 TIMES for($i=0;$i<1000000;$i++) { sizeOf($a); } // STOP TIMER TWO $ResultTwo = benchmarkTimerStop($TimerTwo); // PRINT RESULTS echo 'Count: '.$ResultOne.' Seconds<br />'; echo 'SizeOf: '.$ResultTwo.' Seconds'; // TIMER FUNCTIONS function benchmarkTimerStart() { $timeExplode = explode(" ", microtime()); $time = $timeExplode[1] + $timeExplode[0]; return $time; } function benchmarkTimerStop($timer=0) { $timeExplode = explode(" ", microtime()); $time = $timeExplode[1] + $timeExplode[0]; $finish = $time - $timer; $endTime = sprintf("%4.3f", $finish); return $endTime; } ?> |
My test proved that there is no difference at all! sizeof() is just an alias for count().
So I suggest you to use count() – its the original name of this function.
So long,
Andreas Glaser (aka JaZz)
I think your test is not so correct, is not the number of times you call count or sizeof, is the size of the array what has to be big. Making an array of 1M elements and testing count and sizeof is better.
Cheers,
Pablo Pazos Gutierrez
http://pablo.swp.googlepages.com/
Yeah you’re right your way would be more accurate but would also end up with more or less the same result, since sizeOF is an alias :)
Thanks for your nice reply (i will improve my little script soon).
Greetings
ya sizeof() its an alias for count(), bt the difference is dat count() provides a bit more information dan sizeof()
i)if da variable is an array,count returns da number of elements which sizeof even does.
ii)if da variable is nt an array,count returns 1 which sizeof even doesnt return.
iii)if a variable doesnt exists,count returns 0 which sizeof even doesnt return.
@Pablo,
it doesn’t really matter whether you use an array with 1 million elements or an array with 10 elements. Since the function is called a million times, any time difference would add up and eventually show up in the results.
@sunny ghosh,
sizeof() and count() are exactly the same functions. They refer to the same piece of C code in the php source. This means that both count() and sizeof() will return the same values. So what you said about the differences between count() and sizeof() is not true. Here is some proof dumping the return values of both count() and sizeof():
Return values count() vs sizeof():
Filled array:
int(100000)
int(100000)
Empty array:
int(0)
int(0)
Non array:
int(1)
int(1)
Non existing variable:
int(0)
int(0)
As you can see, all values are the same.
Lastly @ Andreas,
You suggest to use count() instead of sizeof(), but since both functions have the same speed there is no real argument as to why using count(). count() isn’t exactly the “official name”, since both functions are aliases.
In my opinion it’s up to the programmer which function he uses. For example, someone who used C a lot would rather use sizeof() instead of count().
Jelle said: 2010.02.05 23:24
@Pablo,
it doesn’t really matter whether you use an array with 1 million elements or an array with 10 elements. Since the function is called a million times, any time difference would add up and eventually show up in the results.
@sunny ghosh,
sizeof() and count() are exactly the same functions. They refer to the same piece of C code in the php source. This means that both count() and sizeof() will return the same values. So what you said about the differences between count() and sizeof() is not true. Here is some proof dumping the return values of both count() and sizeof():
Return values count() vs sizeof():
Filled array:
int(100000)
int(100000)
Empty array:
int(0)
int(0)
Non array:
int(1)
int(1)
Non existing variable:
int(0)
int(0)
As you can see, all values are the same.
Lastly @ Andreas,
You suggest to use count() instead of sizeof(), but since both functions have the same speed there is no real argument as to why using count(). count() isn’t exactly the “official name”, since both functions are aliases.
In my opinion it’s up to the programmer which function he uses. For example, someone who used C a lot would rather use sizeof() instead of count().
i’ve read some test of both function and sizeof is the winner, but i’ve forgot the source i read :(
regards