0

Regarding to this thread what is faster: in_array or isset? They agreed that isset is faster than in_array.

But I ran to the issue where my page loads too slow and I var_dump-ed my vars to test it explicitly.

This is my extracted array.

$items = array(66, 68, 9, 68, 66, 57, 57, 66, 66, 66, 66, 68, 66, 48, 49, 14, 55, 57, 49, 105, 57, 57, 48, 105, 57, 66, 67, 57, 97, 67, 67, 50, 68, 57, 50, 51, 69, 71, 57, 57, 67, 69, 50, 50, 68, 67, 68, 68, 45, 97, 57, 56, 69, 69, 50, 67, 14, 68, 52, 53, 56, 62, 96, 96, 54, 62, 62, 68, 71, 69, 98, 83, 57, 98, 56, 84, 54, 6, 63, 64, 64, 62, 63, 54, 63, 54, 84, 1, 64, 64, 84, 54, 84, 84, 14, 3, 90, 65, 15, 15, 15, 63, 15, 93, 90, 90, 96, 93, 34, 94, 34, 74, 13, 40, 74, 40, 93, 93, 93, 93, 95, 94, 36, 35, 41, 94, 94, 35, 36, 41, 42, 44, 42, 93, 93, 96, 93, 36, 569, 43, 44, 65, 35, 13, 17, 33, 7, 7, 7, 7, 8, 73, 10, 12, 1, 1, 12, 1, 12, 38, 32, 39, 79, 80, 16, 82, 72, 82, 81, 72, 82, 18, 81, 28, 27, 27, 5, 26, 27, 25, 18, 26, 20, 26, 27, 28, 31, 20, 24, 28, 27, 20, 30, 29, 23, 22, 22, 29, 22, 30, 23, 23, 48, 49, 22, 29, 20, 30, 23, 18, 25, 31, 24, 28, 27, 26, 50, 57, 15, 62, 66, 63, 67, 64, 68, 65, 69, 34, 36, 35, 13, 14, 1, 3, 103, 74, 72, 81, 82, 90, 93, 94, 95, 96, 97, 98, 20, 25, 34, 36, 35, 8, 81, 73, 99, 100, 101, 102, 103, 104, 568, 568);
$productIds = array();

And the result I got is exactly the opposite, which:

$start = microtime(true);
foreach ($items as $item)
{
    //if (!in_array($item, $productIds)) // 0.00030207633972168 seconds
    if(!isset($productIds[$item])) // 5.2928924560547E-5 seconds
    {
        $productIds[] = $item;
    }
}
$end = microtime(true);
echo ($end - $start).' seconds';

Then of course I am gonna stick with in_array in this case. But I am just curious what's going on here.. any idea?

Edit (actual code)

public function validateCreation () 
{
    if ($this->getRequestParameter('ccItems'))
    {
        $arrProductIds = array();
        foreach ($this->getRequestParameter('ccItems') as $ccItem)
        {
            //if ((!is_null($ccItem["product_id"])) && !isset($arrProductIds[$ccItem["product_id"]])) // 12.758,9kb-1.194ms and 12.758,9kb-1.202ms
            if ((!is_null($ccItem["product_id"])) && !in_array($ccItem["product_id"], $arrProductIds)) // 11.599,5kb-972ms and 11.599.5kb-959ms
            {
                $arrProductIds[] = $ccItem["product_id"];
            }
        }
        $result = "";
        if (count($arrProductIds) > 0)
        {
            $isRequestValidated = MyClass::StaticFunction($arrProductIds, $result);
            if ($isRequestValidated === false)
            {
                $this->getRequest()->setError('overall_error', $result);
            }
        }
    }
    return !($this->getRequest()->hasErrors());
}
Community
  • 1
  • 1
choz
  • 17,242
  • 4
  • 53
  • 73
  • 8
    0.00030207633972168 > 5.2928924560547E-5 ... Anyway, a single test like that doesn't prove anything, many other parameters could change the perfs. – xlecoustillier Jul 31 '15 at 08:43
  • Well in my actual page in_array is still faster than isset. The memories returned '12.758,9kb - 1.194ms' for isset and '11.599,5kb - 972ms' for in_array. Well that just proves that isset is not ALWAYS faster than in_array then.. – choz Jul 31 '15 at 08:50
  • @X.L.Ant You're right, in this sample code, isset is still faster.. I will try to comprehend my code properly and analyze it a bit more so i can provide a proper example.. – choz Jul 31 '15 at 09:00
  • 2
    Your `isset($productIds[66])` may almost always return false even after you run `$productIds[]=66;`, because that only produces `[0=>66]`, not `[66=>66]`. – Passerby Jul 31 '15 at 09:01
  • @Passerby in real code '$items' is actually getting request parameters and in my if condition, I call it like '$item['product_id']' and yet ive been arguing with workmates that isset is faster :( – choz Jul 31 '15 at 09:08
  • 2
    @choz I'm not sure if you understand - after the first 3 items, your `$productIds` will look like `[0=>66,1=>68,2=>9]`, so for the fourth item 68, `isset($productsIds[68])` will still return false, because `$productIds[68]` does not exist (only `$productIds[0]`,`$productIds[1]` and `$productIds[2]` exist), but `in_array(68,$productIds)` will return true, because 68 does exist in `$productIds`. – Passerby Jul 31 '15 at 09:12
  • 1
    Your page is **not** loading slow because of `is_array()` or `isset()`. Check the database queries, they are the usual reason. And no, `is_array()` is not faster than `isset()` and your benchmark proves that. – axiac Jul 31 '15 at 09:17
  • @Passerby Oh you're right. But why does my code work in my real app. I shoulda checked it as a string instead of int, i guess.. – choz Jul 31 '15 at 09:24
  • @axiac Nothing to do with database query in this case. – choz Jul 31 '15 at 09:24
  • 1
    @choz That depends on how you claim something "works". If you dump your `$productIds` you will notice **many** duplications. But all the needed data are inside that result array, so maybe your application doesn't break because of duplication (like your demo -- no error is raised), but still you're doing it wrong. – Passerby Jul 31 '15 at 09:36
  • @Passerby So I figured, isset was slower because it had to run the function more times than in_array since what i searched was an int. I've misinterpreted this question, yet I got the answer.. Thanks :) – choz Jul 31 '15 at 09:57

1 Answers1

0

You're wrong.

  • in_array takes 0.00030207633972168 seconds
  • isset takes 5.2928924560547E-5 seconds. Note the E-5 at the end, which by convention means 5.2928924560547×10-5 = 0.000052928924560547, which is faster.
Sebastien C.
  • 4,649
  • 1
  • 21
  • 32
  • I never said it 5.2928924560547E-5 is slower, and I think Passerby comment made more sense to prove why isset is slower. – choz Jul 31 '15 at 09:43