| 1 | import os |
| 2 | import strings |
| 3 | import benchmark |
| 4 | |
| 5 | const max_iterations = os.getenv_opt('MAX_ITERATIONS') or { '100_000' }.int() |
| 6 | |
| 7 | fn imin(x u16, y u16) u16 { |
| 8 | return if x < y { x } else { y } |
| 9 | } |
| 10 | |
| 11 | // From https://gist.github.com/zeozeozeo/f785910173f3115163bffd0c5240de07 |
| 12 | @[direct_array_access] |
| 13 | pub fn zeozeozeo_levenshtein_distance(a string, b string) int { |
| 14 | if a == '' { |
| 15 | return b.len |
| 16 | } |
| 17 | if b == '' { |
| 18 | return a.len |
| 19 | } |
| 20 | if a == b { |
| 21 | return 0 |
| 22 | } |
| 23 | |
| 24 | mut row := []u16{len: a.len + 1} |
| 25 | for i in 1 .. row.len { |
| 26 | row[i] = i |
| 27 | } |
| 28 | |
| 29 | for i := 1; i < b.len; i++ { |
| 30 | mut prev := u16(i) |
| 31 | for j := 1; j < a.len; j++ { |
| 32 | mut current := row[j - 1] // match |
| 33 | if b[i - 1] != a[j - 1] { |
| 34 | // insertion, substitution, deletion |
| 35 | current = imin(imin(row[j - 1] + 1, prev + 1), row[j] + 1) |
| 36 | } |
| 37 | row[j - 1] = prev |
| 38 | prev = current |
| 39 | } |
| 40 | row[a.len] = prev |
| 41 | } |
| 42 | |
| 43 | return row[a.len] |
| 44 | } |
| 45 | |
| 46 | fn main() { |
| 47 | a := 'abcdef' |
| 48 | b := 'abdef' |
| 49 | |
| 50 | mut sum := i64(0) |
| 51 | mut bench := benchmark.start() |
| 52 | sum = 0 |
| 53 | for _ in 0 .. max_iterations { |
| 54 | sum += i64(strings.levenshtein_distance(a, b)) |
| 55 | } |
| 56 | bench.measure('strings.levenshtein_distance: ${sum}') |
| 57 | |
| 58 | sum = 0 |
| 59 | for _ in 0 .. max_iterations { |
| 60 | sum += i64(zeozeozeo_levenshtein_distance(a, b)) |
| 61 | } |
| 62 | bench.measure('zeozeozeo_levenshtein_distance: ${sum}') |
| 63 | |
| 64 | mut fsum := f64(0) |
| 65 | for _ in 0 .. max_iterations { |
| 66 | fsum += strings.dice_coefficient(a, b) |
| 67 | } |
| 68 | bench.measure('strings.dice_coefficient: ${fsum}') |
| 69 | } |
| 70 | |