what is the diffrence between count and sizeof functions of array
The sizeof() function is an alias for count() but I found that sizeof() little bit faster than count.
Example :
<?php
// CREATE TEST ARRAY
$a = array('a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j',
'a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d',
'e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','
f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g',
'h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j',
'a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e',
'f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a',
'b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g',
'h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d',
'e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a'
,'b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','
h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d',
'e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a','b','c','d','e','f','g','h','i','j','a');
// 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';
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;
}
?>
Jennifer
14th April 2025
Understanding the difference between count() and sizeof() can prevent a lot of errors in array handling in this coding similar to in solitaired paying attention to the small details often makes the biggest difference in the end.
Jaxon
19th April 2024
I could say that I regret not working with them for my previous projects. I could have saved a lot and used unique doors. Check Caldwells' site https://caldwells.com/.
Post Your Message