|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | + |
| 3 | +namespace F23.StringSimilarity.Benchmarks; |
| 4 | + |
| 5 | +[MemoryDiagnoser] |
| 6 | +public class Benchmarks |
| 7 | +{ |
| 8 | + [Benchmark] |
| 9 | + public void Cosine() |
| 10 | + { |
| 11 | + var cosine = new Cosine(); |
| 12 | + _ = cosine.Distance("hello", "world"); |
| 13 | + } |
| 14 | + |
| 15 | + [Benchmark] |
| 16 | + public void Damerau() |
| 17 | + { |
| 18 | + var damerau = new Damerau(); |
| 19 | + _ = damerau.Distance("hello", "world"); |
| 20 | + } |
| 21 | + |
| 22 | + [Benchmark] |
| 23 | + public void Jaccard() |
| 24 | + { |
| 25 | + var jaccard = new Jaccard(); |
| 26 | + _ = jaccard.Distance("hello", "world"); |
| 27 | + } |
| 28 | + |
| 29 | + [Benchmark] |
| 30 | + public void JaroWinkler() |
| 31 | + { |
| 32 | + var jaro = new JaroWinkler(); |
| 33 | + _ = jaro.Distance("hello", "world"); |
| 34 | + } |
| 35 | + |
| 36 | + [Benchmark] |
| 37 | + public void Levenshtein() |
| 38 | + { |
| 39 | + var levenshtein = new Levenshtein(); |
| 40 | + _ = levenshtein.Distance("hello", "world"); |
| 41 | + } |
| 42 | + |
| 43 | + [Benchmark] |
| 44 | + public void LongestCommonSubsequence() |
| 45 | + { |
| 46 | + var lcs = new LongestCommonSubsequence(); |
| 47 | + _ = lcs.Distance("hello", "world"); |
| 48 | + } |
| 49 | + |
| 50 | + [Benchmark] |
| 51 | + public void MetricLCS() |
| 52 | + { |
| 53 | + var metricLcs = new MetricLCS(); |
| 54 | + _ = metricLcs.Distance("hello", "world"); |
| 55 | + } |
| 56 | + |
| 57 | + [Benchmark] |
| 58 | + public void NGram() |
| 59 | + { |
| 60 | + var ngram = new NGram(); |
| 61 | + _ = ngram.Distance("hello", "world"); |
| 62 | + } |
| 63 | + |
| 64 | + [Benchmark] |
| 65 | + public void NormalizedLevenshtein() |
| 66 | + { |
| 67 | + var normalizedLevenshtein = new NormalizedLevenshtein(); |
| 68 | + _ = normalizedLevenshtein.Distance("hello", "world"); |
| 69 | + } |
| 70 | + |
| 71 | + [Benchmark] |
| 72 | + public void OptimalStringAlignment() |
| 73 | + { |
| 74 | + var osa = new OptimalStringAlignment(); |
| 75 | + _ = osa.Distance("hello", "world"); |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark] |
| 79 | + public void QGram() |
| 80 | + { |
| 81 | + var qGram = new QGram(); |
| 82 | + _ = qGram.Distance("hello", "world"); |
| 83 | + } |
| 84 | + |
| 85 | + [Benchmark] |
| 86 | + public void RatcliffObershelp() |
| 87 | + { |
| 88 | + var ratcliffObershelp = new RatcliffObershelp(); |
| 89 | + _ = ratcliffObershelp.Distance("hello", "world"); |
| 90 | + } |
| 91 | + |
| 92 | + [Benchmark] |
| 93 | + public void SorensenDice() |
| 94 | + { |
| 95 | + var sorensenDice = new SorensenDice(); |
| 96 | + _ = sorensenDice.Distance("hello", "world"); |
| 97 | + } |
| 98 | + |
| 99 | + [Benchmark] |
| 100 | + public void WeightedLevenshtein() |
| 101 | + { |
| 102 | + var weightedLevenshtein = new WeightedLevenshtein(new ExampleCharSub()); |
| 103 | + _ = weightedLevenshtein.Distance("hello", "world"); |
| 104 | + } |
| 105 | + |
| 106 | + private class ExampleCharSub : ICharacterSubstitution |
| 107 | + { |
| 108 | + public double Cost(char c1, char c2) |
| 109 | + { |
| 110 | + // The cost for substituting 't' and 'r' is considered smaller as these 2 are located next to each other on a keyboard |
| 111 | + if (c1 == 't' && c2 == 'r') return 0.5; |
| 112 | + |
| 113 | + // For most cases, the cost of substituting 2 characters is 1.0 |
| 114 | + return 1.0; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments