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;
}

?>

 

0   0
Surya Pratap
profile 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.

Write a comment ...
Post comment
Cancel
profile 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/.

Write a comment ...
Post comment
Cancel

Post Your Message

Please rotate your device

We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site