PHP Doku:: Alias von count - function.sizeof.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenArraysArray Funktionensizeof

Ein Service von Reinhard Neidl - Webprogrammierung.

Array Funktionen

<<shuffle

sort>>

sizeof

(PHP 4, PHP 5)

sizeofAlias von count()

Beschreibung

Diese Funktion ist ein Alias für: count().


8 BenutzerBeiträge:
- Beiträge aktualisieren...
Emil Mohamed
22.12.2008 10:04
Here are real test for below statements:
Source:
<?php
$arr
= array();
$num = 1000000;
echo
"Populating test array with $num elements ... ";
for(
$i=0; $i<$num; $i++) {
   
$arr[$i] = microtime(true) . mt_rand();
}
echo
"Populated.\n\n";

echo
"Start testing FOREAH ... \n";
$start_time = microtime(true);
foreach (
$arr as $i=>$elem) {
   
$res = isset($arr[$i]);
}
echo
"FOREACH result: " . (microtime(true) - $start_time) . "\n\n";

echo
"Start testing regular FOR ... \n";
$start_time = microtime(true);
for (
$i=0; $i<sizeof($arr); $i++) {
   
$res = isset($arr[$i]);
}
echo
"Regular for result: " . (microtime(true) - $start_time) . "\n\n";

echo
"Starting test for pre-computed size array FOR ... \n";
$start_time = microtime(true);
$sizeof_arr = sizeof($arr);
for (
$i=0; $i<$sizeof_arr; $i++) {
   
$res = isset($arr[$i]);
}
echo
"pre-computed size of array FOR: " . (microtime(true) - $start_time);
?>

Output:
emil@neon:~$ php test-loops-performance.php
Populating test array with 1000000 elements ... Populated.

Start testing FOREAH ...
FOREACH result: 0.500122070312

Start testing regular FOR ...
Regular for result: 0.886044979095

Starting test for pre-computed size array FOR ...
pre-computed size of array FOR: 0.454435825348
umopdn [at] msn [dawt] com
23.09.2008 23:38
a) Always try and use PHP's internal routines to iterate through objects of various types (arrays in most examples below).

Instead of interpreting your code to loop through them, they use their own internal routines which are much faster.

(This is why foreach () will run faster than manual interation)

b) It is _always_ good practice to leave as many static resulting functions outside of loops, having operations that return the exact same piece of data every iteration of the loop is not pretty on resources.

c) I agree with PixEye's remarks on sizeof().  In PHP it is just an alias for the true function count().  It has other meanings logically in other languages rather than the number of elements in an object.  This should be avoided as it may confuse developers transitioning to PHP from other languages.
PixEye
23.09.2008 18:09
I am quite surprised about previous posts. Here are my advices:

1/ prefer the count() function instead of sizeOf() as sizeOf() is only an alias of count() and does not mean the same in many other languages based on C (avoid ambiguity).

2/ prefer the powerful forEach() function to iterate over arrays.
kaisellgren at gmail dot com
7.09.2008 17:54
In addition to previous posts, there is a better and cleaner way to loop through an array using this function.

<?php

for ($a = 0,$b = sizeof($array);$a < $b;$a++)
 echo
$array[$a];

?>
rrf5000 at psu dot edu
17.04.2008 16:54
To explain previous comments:  the reason that the FOR loop runs faster on LARGE arrays if you set its size to an array first (using count() or sizeof() ) is pretty simple.  It's because if you use

<?php
for ($i = 0; $i < sizeof($huge_array); $i++)
{
 
code
}
?>

every time the FOR loop checks its condition (is $i less than sizeof($huge_array) ), it must run the sizeof() function on $huge_array again.  This is what causes the extra processing time, which can become significant on larger arrays.

As a side note, this has been thoroughly commented on both correctly and incorrectly in the User Contributed Notes for the count() function.  Users Jaik, Wulfson, and Anonymous have summed this particular matter up pretty well in their posts http://www.php.net/manual/en/function.count.php#78741 , http://www.php.net/manual/en/function.count.php#78931 , and http://www.php.net/manual/en/function.count.php#79238 .
Andreas dot Schuhmacher87 at googlemail dot com
13.04.2008 20:02
also you can use for like this (no count() needed)
<?php
   
for($i = 0, $item = ''; false != ($item = $some_array[$i]); $i++) {
        print
$item; //index $i of $some_array
        //code
   
}
?>
communiti.ch
10.04.2008 15:55
If your array is "huge"

It is reccomended to set a variable first for this case:

THIS->

$max = sizeof($huge_array);
for($i = 0; $i < $max;$i++)
{
code...
}

IS QUICKER THEN->

for($i = 0; $i < sizeof($huge_array);$i++)
{
code...
}
svanegmond at tinyplanet dot ca
13.03.2008 20:12
For people who use this kind of idiom:

for ($i=0; $i<sizeof($array); $i++) {
   ...
}

At least in PHP4, you will notice a large performance improvement if you switch to:

$max = sizeof($array);
for ($i=0; $i<$max; $i++) {
   ...
}

I had an array with a few thousand items in it, each of them a map with 5 key-value pairs, and just for'ing through the array took a second of CPU on a decent machine.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)