Skip to content

Commit 792812d

Browse files
staabmsebastianbergmann
authored andcommitted
Perf optimize MemoryEfficientLongestCommonSubsequenceCalculator
1 parent 7e53eb5 commit 792812d

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/MemoryEfficientLongestCommonSubsequenceCalculator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ private function length(array $from, array $to): array
7878
if ($from[$i] === $to[$j]) {
7979
$current[$j + 1] = $prev[$j] + 1;
8080
} else {
81-
$current[$j + 1] = max($current[$j], $prev[$j + 1]);
81+
// don't use max() to avoid function call overhead
82+
if ($current[$j] > $prev[$j + 1]) {
83+
$current[$j + 1] = $current[$j];
84+
} else {
85+
$current[$j + 1] = $prev[$j + 1];
86+
}
8287
}
8388
}
8489
}

0 commit comments

Comments
 (0)