[php-src] PR #13830: Optimize levenshtein a bit for memory usage
| From: | nielsdos | Date: | Thu, 28 Mar 2024 22:18:44 +0000 |
| Subject: | [php-src] PR #13830: Optimize levenshtein a bit for memory usage | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-25561@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/13830
Author: nielsdos
When all costs are greater than 0, levenshtein fulfills the requirements of being a metric. A metric
is symmetric, so we can swap the strings in that case. Since we use rows of a partial matrix of
length |string2| we can make the choice of using string1 instead if |string1| < |string2|, which
will optimize memory usage and CPU time.
Sample bench script:
```
<?php
for ($i = 0;$i<2000;$i++)
levenshtein(str_repeat('a', 100), str_repeat('b', 1000), 1, 100, 1);
```
results:
```
Benchmark 1: ./sapi/cli/php x.php
Time (mean ± σ): 254.5 ms ± 1.7 ms [User: 251.5 ms, System: 2.2 ms]
Range (min … max): 252.5 ms … 257.4 ms 11 runs
Benchmark 2: ./sapi/cli/php_old x.php
Time (mean ± σ): 292.0 ms ± 2.7 ms [User: 289.8 ms, System: 1.8 ms]
Range (min … max): 287.4 ms … 295.6 ms 10 runs
Summary
./sapi/cli/php x.php ran
1.15 ± 0.01 times faster than ./sapi/cli/php_old x.php
```