0

I have a big set of loops for processing data and checked several SO topics to increase current performance of my code. Thanks to this question, I converted all my stdObjects to array and it already made a difference in terms of performance.

Then I figured out using === (is exactly equal to) is slightly faster than using == (is equal to). (source)

But I also believe that casting variable also requires some resource.

How could I understand if I manage to gain performance by casting a variable to use === instead of ==?

Example code:

foreach ($bigDataSet as $key => $val) {
    $key = (int)$key;

    if ($key === $someInt) {
        //do_some_work (other foreachs like above)
    } elseif ($key === $someIntN) {
        //do_some_work_n (other foreachs like above)
    }
}

Current code:

foreach ($bigDataSet as $key => $val) {

    if ($key == $someTypeWithINTVAL) {
        //do_some_work (other foreachs like above)
    } elseif ($key == $someTypeWithINTVALN) {
        //do_some_work_n (other foreachs like above)
    }
}

Thanks in advance for any guide regarding to this matter.

Pelin
  • 467
  • 4
  • 17
  • Think the other way. `==` tries to cast both to be the same type to be able to compare by value. When using `===` it fails, if types are not equal and then the value. – Markus Zeller Aug 03 '22 at 13:36
  • Also you can improve your loops by **not** using branching or elses. `foreach($array as $key => $value) { if(condition) { doSomething(); continue; } doElseWorkHere(): }` – Markus Zeller Aug 03 '22 at 13:39
  • Thanks @MarkusZeller! I have multiple `elseif`s, would `continue;` help in this scenario aswell? – Pelin Aug 03 '22 at 14:18
  • Premature optimization is the root of all evil. You didn't even defined "huge", neither provided any information on the nature of the *actual* calculations. Which makes this question essentially useless and misleading. – Your Common Sense Aug 03 '22 at 14:27
  • 1
    Given your other question, it seems there is a database interaction involved. which should be your concern instead of that petty difference in PHP operators – Your Common Sense Aug 03 '22 at 14:30
  • I agree with @YourCommonSense. Inefficient queries or nested queries will affect your execution time far, _far_ more than the difference between strict and non strict equality. – CFulford Aug 03 '22 at 15:41
  • 1
    Less hops for a strict comparison. `$a == $b`: 1. check type of `$a`, 2. check type of `$b`, 3. evaluate how to loose compare. `(int) $a === $b`: 1. set type of `$a`, 2. strict compare. – Markus AO Aug 03 '22 at 17:16
  • @YourCommonSense I see, I will try to be more specific about the data and update my question accordingly. Optimization on database side is already done way before than PHP part. Also most of the data processed once are being cached through redis during the entire process for recalls. The app itself is memory hungry and the dataset I am dealing has around 50M records mostly stored as enum, binary - 64 and int - 11 on DB. – Pelin Aug 03 '22 at 19:49
  • @MarkusAO Thank you very much, is there any way that I could measure weight of casting since I will need to cast a lot of stuff. – Pelin Aug 03 '22 at 19:51

0 Answers0