0

I'm using PHP4 for a project I'm working on. I had to expand this code (which works fast enough for the data at hand):

for ($i = 1; $i <= count($arr); $i++) {
$a = $arr[$i]['date'];
    for ($y = 1; $y <= 10000000; $y++) {
      $c = $arr[$i]['data'][$y];
      // here I inserted another loop
    }
}

I inserted another loop in it, like so:

for ($k = $i + 1; $k <= count($arr); $k++) {
    $next_is_empty = false;
    $next_is_null = false;
    if ($arr[$k]['data'][$y] == '') {
        $next_is_empty = true;
        break;
    } elsif (is_null($arr[$k]['data'][$y])) {
        $next_is_null = true;
        break;
    }
}

The code is more a general idea then a specific working code, since I'm writing by memory. However, I think it is accurate enough for my questions. So, this loop I inserted works fine for most cases, but is too slow in some - how could I speed it up? I'm also curios about general rules of performance based on this example. I know that nested loops are better to be avoided, but why for example does the code works fast enough if I put the variables $next_is_empty/null in front of my loop (then the solution is wrong, but fast enough)? I know it needs to make more reassignments, but how come they take so much time?

Mandy
  • 107
  • 1
  • 2
  • 9
  • You seem to have lots of instances of `[i]` - should this be `[$i]` etc. – Nigel Ren Jun 28 '19 at 18:16
  • Also a number of `[k]` and `[y]` which also need to be correct. – Dave Jun 28 '19 at 18:20
  • Could it be that the code is faster when you remove the assignments because if statements become empty and the parser will optimise the loops? – Dharman Jun 28 '19 at 18:24
  • Without specifics of the processing being done - how can we suggest how to best optimise it. There are a wide range of methods that can be used for specific cases, but very few that will work in all cases (and then will usually only provide limited benefits). – Nigel Ren Jun 28 '19 at 18:27
  • 1
    You may find that upgrading the version of PHP may also help - PHP 4 is slightly out of date. – Nigel Ren Jun 28 '19 at 18:34
  • 2
    yikes... PHP 4. I can't even benchmark that thing: https://3v4l.org/LSglc The support for it [ended 10 years ago](https://www.php.net/eol.php). You need to upgrade as your priority instead of worrying about micro-optimisations. – Dharman Jun 28 '19 at 18:40
  • Thank you Nigel Ren and Dave, I corrected it! – Mandy Jun 28 '19 at 20:25
  • Dharman, it is possible! – Mandy Jun 28 '19 at 20:26
  • Nigel Ren and Dharman, I would if I could, I'm just trying to make little adjustments to a program written years ago. – Mandy Jun 28 '19 at 20:28

1 Answers1

3

do not use count() in the loop , make the count() in different line and then use the variable in the loop

$count =  count($arr);
for ($i = 1; $i <= $count; $i++) {
$a = $arr[i]['date'];
    for ($y = 1; $y <= 10000000; $y++) {
      $c = $arr[i]['data'][y];
      // here I inserted another loop
    }
}

this way have better bench-marking result

the point of defining the variable $next_is_empty outside the loop, it difficult to answer as - as far as i know- it depends on the PHP version you use, but better to put the outside , that is the original.

Mohammed Omer
  • 1,168
  • 1
  • 10
  • 17
  • Do you have anything to support this statement? A benchmark or link to documentation? – Dharman Jun 28 '19 at 18:21
  • 1
    check this https://www.electrictoolbox.com/php-for-loop-counting-array/ – Mohammed Omer Jun 28 '19 at 18:25
  • It doesn't look at all trustworthy. There is no mention of the PHP version they used for benchmark, the measurements are very vague and the whole article is 10 years old. – Dharman Jun 28 '19 at 18:27
  • and i think you can try it yourself and measure the time before and after each way of the code – Mohammed Omer Jun 28 '19 at 18:27
  • The problem is that it will make a difference, but when you take into account all of the other code (which we don't know about) this may be very minimal. – Nigel Ren Jun 28 '19 at 18:28
  • i read about it long ago and i just make a quick search to support my words , but as i told you , you can make an extra mile and measure the time or make more search , – Mohammed Omer Jun 28 '19 at 18:29
  • yes it is but it depends on PHP version and overall there is many other aspects, but in this particular code , this is the proper code tuning that i can answer – Mohammed Omer Jun 28 '19 at 19:03