WP_Text_Diff_Renderer_Table::compute_string_distance( string $string1, string $string2 ): int
- Since
- 2.6.0
- Source
wp-includes/class-wp-text-diff-renderer-table.php:493
Compatibility
- WordPress
- since 2.6.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$string1string$string2string
Return value
int
Performance profile
How much work a call to WP_Text_Diff_Renderer_Table::compute_string_distance() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 35–63
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 64.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Text_Diff_Renderer_Table::compute_string_distance() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
isset($count_key1) && isset($count_key2) && isset($difference_key) | 35–37 | md5(), md5(), md5() |
isset($difference_key) | 41–43 | md5(), md5(), count_chars(), md5() |
!isset($count_key1) && !isset($count_key2) && isset($difference_key) | 47–49 | md5(), md5(), count_chars(), count_chars(), md5() |
isset($count_key1) && isset($count_key2) && !isset($difference_key) | 49–51 | md5(), md5(), md5(), array_map() |
!isset($difference_key) | 55–57 | md5(), md5(), count_chars(), md5(), array_map() |
!isset($count_key1) && !isset($count_key2) && !isset($difference_key) | 61–63 | md5(), md5(), count_chars(), count_chars(), md5(), array_map() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 64 | 35–63 | 4 | |
| 8.5 | 64 | 35–63 | 4 | |
| 8.4 | 64 | 35–63 | 4 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 70 | 41–69 | 4 | |
| 8.2 | 70 | 41–69 | 4 | |
| 8.1 | 70 | 41–69 | 4 | |
| 7.4 | 70 | 41–69 | 4 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Used by · 1
- WP_Text_Diff_Renderer_Table::interleave_changed_lines()Takes changed blocks and matches which rows in orig turned into which rows in final.
Source code
public function compute_string_distance( $string1, $string2 ) { // Use an md5 hash of the strings for a count cache, as it's fast to generate, and collisions aren't a concern. $count_key1 = md5( $string1 ); $count_key2 = md5( $string2 ); // Cache vectors containing character frequency for all chars in each string. if ( ! isset( $this->count_cache[ $count_key1 ] ) ) { $this->count_cache[ $count_key1 ] = count_chars( $string1 ); } if ( ! isset( $this->count_cache[ $count_key2 ] ) ) { $this->count_cache[ $count_key2 ] = count_chars( $string2 ); } $chars1 = $this->count_cache[ $count_key1 ]; $chars2 = $this->count_cache[ $count_key2 ]; $difference_key = md5( implode( ',', $chars1 ) . ':' . implode( ',', $chars2 ) ); if ( ! isset( $this->difference_cache[ $difference_key ] ) ) { // L1-norm of difference vector. $this->difference_cache[ $difference_key ] = array_sum( array_map( array( $this, 'difference' ), $chars1, $chars2 ) ); } $difference = $this->difference_cache[ $difference_key ]; // $string1 has zero length? Odd. Give huge penalty by not dividing. if ( ! $string1 ) { return $difference; } // Return distance per character (of string1). return $difference / strlen( $string1 ); }Changelog
Introduced in 2.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/class-wp-text-diff-renderer-table.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.