View Issue Details
| ID | Project | Category | View Status | Date Submitted | Last Update |
|---|---|---|---|---|---|
| 0005742 | Composr | core | public | 2024-04-28 19:16 | 2024-07-22 21:22 |
| Reporter | Patrick Schmalstig | Assigned To | |||
| Severity | Feature-request | ||||
| Status | non-assigned | Resolution | open | ||
| Product Version | |||||
| Fixed in Version | |||||
| Summary | 0005742: Consider in_array when there are 3 or more string comparisons | ||||
| Description | in_array is more readable and possibly faster if comparing 3 or more strings with the same condition in an if guard. It can also be used for strict comparison of multiple strings (with in_array parameter 3 being set to true). Consider adding this to coding standards / code linter. | ||||
| Tags | Roadmap: Over the horizon, Type: Performance | ||||
| Time estimation (hours) | |||||
| Sponsorship open | |||||
|
|
Hmm, I'm surprised but it is faster... <?php define('ITERATIONS', 10000000); $x = 'hello'; $a = microtime(true); for ($i = 0; $i < ITERATIONS; $i++) { if ($x == 'a' || $x == 'b' || $x == 'c') { } } $b = microtime(true); var_dump($b - $a); $a = microtime(true); for ($i = 0; $i < ITERATIONS; $i++) { if (in_array($x, ['a', 'b', 'c'])) { } } $b = microtime(true); var_dump($b - $a); float(0.28515386581421) float(0.10288000106812) |
| Date Modified | Username | Field | Change |
|---|---|---|---|
| 2024-04-28 19:16 | Patrick Schmalstig | New Issue | |
| 2024-04-28 19:16 | Patrick Schmalstig | Tag Attached: Roadmap: Over the horizon | |
| 2024-07-22 21:22 | Chris Graham | Tag Attached: Type: Performance | |
| 2024-07-22 21:22 | Chris Graham | Note Added: 0008877 |