Created
May 20, 2020 10:15
-
-
Save rikkimax/87545488b90cd06de0d69ed204d42343 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mine = mine; | |
import original = original; | |
import std.stdio; | |
import std.datetime.stopwatch; | |
void main() { | |
import core.memory; | |
GC.disable; | |
auto times = benchmark!(() { | |
string a = "bbc", b = "aa"; | |
ulong result = original.levenshteinEditDistanceStr(a, b); | |
assert(result == 3); | |
}, () { | |
string a = "aa", b = "bbc"; | |
ulong result = original.levenshteinEditDistanceStr(a, b); | |
assert(result == 3); | |
},() { | |
ulong result = mine.levenshteinEditDistanceStr("aa", "bbc"); | |
assert(result == 3); | |
}, () { | |
ulong result = mine.levenshteinEditDistanceStr("bbc", "aa"); | |
assert(result == 3); | |
})(1_000_000); | |
writeln(times[0], "\t", times[0] / 1_000_000); | |
writeln(times[1], "\t", times[1] / 1_000_000); | |
writeln(times[2], "\t", times[2] / 1_000_000); | |
writeln(times[3], "\t", times[3] / 1_000_000); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module mine; | |
import std; | |
alias levenshteinEditDistanceStr = levenshteinEditDistance !string; | |
ulong levenshteinEditDistance(T)(T a, T b) if (isSomeString !T) { | |
import std.algorithm : min; | |
ulong[][] matrix; | |
matrix.length = a.length + 1; | |
foreach (ref v; matrix) | |
v.length = b.length + 1; | |
foreach (i; 0..a.length + 1) | |
matrix[i][0] = i; | |
foreach (j; 0..b.length + 1) | |
matrix[0][j] = j; | |
foreach (iMinice1, previousA; a) { | |
size_t i = iMinice1 + 1; | |
foreach (jMinice1, previousB; b) { | |
size_t j = jMinice1 + 1; | |
ulong substitutionCost = cast(ulong)(previousA != previousB); | |
matrix[i][j] = min(matrix[iMinice1][jMinice1] + substitutionCost, | |
matrix[i][jMinice1] + 1, matrix[iMinice1][j] + 1); | |
} | |
} | |
return matrix[a.length][b.length]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module original; | |
import std; | |
alias levenshteinEditDistanceStr=levenshteinEditDistance!string; | |
ulong levenshteinEditDistance(T)(in ref T a, in ref T b) if (isSomeString!T) { | |
import std.array : uninitializedArray; | |
auto matrix = uninitializedArray!(ulong[][])(a.length + 1, b.length + 1); | |
foreach (i; 0 .. a.length + 1) | |
matrix[i][0] = i; | |
foreach (j; 0 .. b.length + 1) | |
matrix[0][j] = j; | |
import std.algorithm : min; | |
for (int i = 1; i < a.length + 1; ++i) | |
for (int j = 1; j < b.length + 1; ++j) { | |
const ulong substitutionCost = a[i - 1] == b[j - 1] ? 0 : 1; | |
matrix[i][j] = min(matrix[i - 1][j - 1] + substitutionCost, | |
matrix[i][j - 1] + 1, matrix[i - 1][j] + 1); | |
} | |
return matrix[a.length][b.length]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment