275

To find the number of elements in a PHP $array, which is faster/better/stronger?

count($array) or sizeof($array) ?

Edit

Thanks to Andy Lester, I have refined my question to mean from a multilingual perspective. The manual commenters say

"[sizeof] does not mean the same in many other languages based on C"

Is this true?

1
  • 2
    Very nice question. But here is a good benchmark site, that says sizeof is just a 21µs faster Commented Aug 10, 2013 at 16:11

7 Answers 7

236

I would use count() if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof(), what is that?" and having to consult the documentation.

I think it means sizeof() does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen(), printf(), etc)

Sign up to request clarification or add additional context in comments.

1 Comment

what is that?" and having to consult the documentation. that's what brought me here lol
118

According to phpbench:

Is it worth the effort to calculate the length of the loop in advance?

//pre-calculate the size of array
$size = count($x); //or $size = sizeOf($x);

for ($i=0; $i<$size; $i++) {
    //...
}

//don't pre-calculate
for ($i=0; $i<count($x); $i++) { //or $i<sizeOf($x);
    //...
}

A loop with 1000 keys with 1 byte values are given.

count() sizeof()
With precalc 152 212
Without precalc 70401 50644

(time in µs)

So I personally prefer to use count() instead of sizeof() with pre calc.

7 Comments

Please tell only which takes less time. Else the answer is quite complicated and confusing too! Thanks
what version of php were you testing with? Newer versions have a lot of optimisations which may make these results quite different
why is sizeof faster without saving it's result into a variable but otherwise it's the other way around? is this some bad design choice in the php interpreter that causes some sort of selective overhead? i would like to know how you benchmarked this, the microsecond measurement worries me. maybe try running this a huge amount of times in an outer for loop?
@rubo77 See the above code; "pre-calc" is determining the size of the array once, before the for-loop. Without "pre-calc" is determining the size of the array inside the for-loop
This answer shows the benefit of caching the count in a variable before a loop. I fail to see how this is relevant to the question.
|
44

They are identical according to sizeof()

In the absence of any reason to worry about "faster", always optimize for the human. Which makes more sense to the human reader?

Comments

24

According to the website, sizeof() is an alias of count(), so they should be running the same code. Perhaps sizeof() has a little bit of overhead because it needs to resolve it to count()? It should be very minimal though.

1 Comment

Well, sorry for guessing? I was just stating that they're actually the same thing. Then, I made a logical inference that using an alias for a function instead of the actual function might be very minimally slower, but admited that i really have no idea by prefacing it with the word "perhaps." Sorry if that offended you. Maybe next time i'll use the word "infinitesimal."
16

I know this is old but just wanted to mention that I tried this with PHP 7.2:

<?php
//Creating array with 1 000 000 elements
$a = array();
for ($i = 0; $i < 1000000; ++$i)
{
    $a[] = 100;
}

//Measure
$time = time();
for ($i = 0; $i < 1000000000; ++$i)
{
    $b = count($a);
}
print("1 000 000 000 iteration of count() took ".(time()-$time)." sec\n");

$time = time();
for ($i = 0; $i < 1000000000; ++$i)
{
    $b = sizeof($a);
}
print("1 000 000 000 iteration of sizeof() took ".(time()-$time)." sec\n");
?>

and the result was:

1 000 000 000 iteration of count() took 414 sec
1 000 000 000 iteration of sizeof() took 1369 sec

So just use count().

UPDATE:

On PHP 8.3 the speed is identical:

1 000 000 000 iteration of count() took 10 sec
1 000 000 000 iteration of sizeof() took 10 sec

4 Comments

This is interesting. What PHP version do you use?
@waza I'm using PHP 7.2
With PHP 7.3.3 : 1 000 000 000 iteration of count() took 525 sec 1 000 000 000 iteration of sizeof() took 1361 sec
with PHP 7.0 and ubuntu machine 1 000 000 000 iteration of count() took 38 sec 1 000 000 000 iteration of sizeof() took 46 sec
7

sizeof() is just an alias of count() as mentioned here

http://php.net/manual/en/function.sizeof.php

1 Comment

+alex yes it does. I see two links. Did you expect him to rewrite sizeof ? LOL :p
-2

Both are used to count elements in a array. sizeof() function is an alias of count() function used in PHP. However, count() function is faster and better than sizeof().

1 Comment

Other answers like this were already saying this. Why repeat the same info?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.