phploopscalculation# Is there a better way to reduce number of iterations without missing a given condition?

I'm working on a project which requires a lot of iterations and calculations. We're speaking about something around 2000 iterations, each of them doing between 15 and 40 different calculations. This.. takes a lot of time, and I'm trying to reduce that time.

I'll explain how it works as clearly as I can. It's about finding the best heat exchanger solution for given inputs (inlet temperature, pressure, power, stuff like that). To do so, I must compare all the different plates (each plates have different size, different properties), and they have four important things : a type (A, B or mixed), a fraction (it'll be 100% if the type is A, 0 if the type is B, and any multiple of 5 between 0 and 100, except 50, for the mixed type) a min number, and a max number.

So now let's say we have a plate which type is A, min number is 7 and max number is 93. Fraction will be 100% since it's defined by the plate type. In this case, my loop will look like this :

```
for ($nb = $plate['nb_min']; $nb <= $plate['nb_max']; $nb += 2) { ... }
```

Which will already do 44 iterations.

If a plate's type is B, the loop will be similar. But in the case of the mixed one (with the same min and max nb), I have to do this :

```
for ($nb = $plate['nb_min']; $nb <= $plate['nb_max']; $nb += 2) {
for ($frac = 5; $frac <= 95; frac += 5) {
...
}
}
```

Which results in 792 iterations. For a single plate, and there are many more.

The condition which break the iterations (we stop at the first result for each plate so that we have the margin the closest to 0, and the cheapest solution - the more plates there is, the more expensive the solution is) is if a margin is positive and if the differential pressures are in acceptable range (the user defines this range with an input) :

```
if ($margin > 0 && $dp_prim < $postData['dp_max_prim'] && $dp_sec < $postData['dp_max_sec']) {
// current iteration is added to the array of solutions, loop is broken and we can iterate throught the next plate
}
```

So naturally I tried to read all the margins for each iterations, to see if I could just notice a pattern in it. But it failed, plates are very different, and the difference of margin for different numbers of plate is never the same.

What I mean is that the margin for a plate can be -70, then -67 on the next iteration, then -60, etc. But for another plate, it can be -70, then -30, etc.

One thing I noticed is that the number of plates is *generally* in the higher numbers of plates. So I tried to inverse the loop, starting with the max nb and going down. It gave me way faster results, but obviously the margin is super high and the price as well, since it's not the very first solution to reach a positive margin.

Another "solution" I found, is that I just skip some iterations if the margin is too low. Like this :

```
if ($margin < -50) {
$nb += 6;
continue;
}
if ($margin < -30) {
$nb += 2;
continue;
}
```

But still, there's a risk that I totally miss the margin closest to 0. I have no idea on how to correctly reduce the calculation time / iteration number and still getting the first positive margin.

Solution

What I did, as suggested by user Martin Brown, is a partial binary search. Basically, I take the value in the middle of the array, apply my function to it, and according to the result, I take the top or bottom array as my new array to iterate on. I did this step twice, and the calculation time literally halved.

I couldn't do it forever because as suggested in my post I absolutely must find the first value which meets the conditions, and many values do meet them.

- Use php money format and remove the numbers after the decimal place for GBP
- Edmonds maximum matching algorithm in PHP
- Custom Laravel validation messages
- Why does this false value convert to empty string instead of 0?
- Customize completely WooCommerce My account Orders section
- PHP function use(*) all
- Get contents from a json page from habbo
- Load a .env file with PHP
- Doctrine proxy getter makes PHP app down with segmentation fault
- List of All Locales and Their Short Codes?
- How to display PHP errors in code output?
- How to automatically rename file name to username value for uploaded user images in WordPress - ultimate memeber plugin
- Calling REST API from controller codeigniter
- AJAX/JQUERY/PHP issue
- unable to add multiple .JS files to template.php in Drupal 7
- Why after adding /spatie-laravel-feed plugin nothig is opened?
- Zend for picture uploads needing to resize images
- WordPress wp_enqueue_media() upload_mimes not filtering
- wordpress custom post types last four
- Using two tables. What am I doing wrong here?
- How to replace URL and pass data in behat
- Codeigniter - $sOrder and $sLimit in datatables
- Call php function onclick button in same page
- Can't access old input and data is lost with Laravel
- Magento | Update Item Price in Quote Loop
- Determining XSS Attack Vulnerabilities
- on editing file upload field is blank
- Where do you store user uploaded content within a Symfony 4 application?
- Cakephp 3: how to implement events in helper
- Display name instead of id in url laravel