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

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