diff --git a/README.md b/README.md index 9db48ee..5483632 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,11 @@ memory, to serve as a buffer for storing results. 1. Make benchmarks. 2. Introduce more tests. - 2.1. Tests with pre, post and infix strings shared between the strings - 2.2 Tests where the length of the strings are combinations of odd and - even. -3. Reduce the size of the buffer. When this was done with the old version, - performance was increased 100%. + 2.1 Tests with pre, post and infix strings shared between the strings + 2.2 Tests where the length of the strings are combinations of odd and + even. +3. ~~Reduce the size of the buffer. When this was done with the old version, + performance was increased 100%.~~ 4. Look into SIMD instructions 5. Look into parallelism. 6. Templating the code diff --git a/src/iosifovitch.cpp b/src/iosifovitch.cpp index e830808..465250c 100644 --- a/src/iosifovitch.cpp +++ b/src/iosifovitch.cpp @@ -16,26 +16,25 @@ auto levenshtein_distance(std::string_view const& a, std::string_view const& b) if (i != 0) return levenshtein_distance(a.substr(0, a.size() - i), b.substr(0, b.size() - i)); auto const buffer_length = a.size() + 1; - auto buffers = new unsigned int[buffer_length * 2]; + auto buffer = new unsigned int[buffer_length]; - unsigned int * buffer[2] = {buffers, buffers + buffer_length}; - std::iota(buffer[0], buffer[1], 0); - std::fill(buffer[1], buffer[1] + buffer_length, 0); + std::iota(buffer, buffer + buffer_length, 0); for (auto i = 0u; i < b.size(); ++i) { - buffer[1][0] = i + 1; + buffer[0] = i; + auto temp = i; for (auto j = 0u; j < a.size(); ++j) { - buffer[1][j + 1] = std::min( - buffer[0][j] + (a[j] == b[i] ? 0u : 1u), - std::min(buffer[0][j + 1], buffer[1][j]) + 1u + temp = std::min( + temp + (a[j] == b[i] ? 0u : 1u), + std::min(buffer[j + 1], buffer[j]) + 1u ); + std::swap(buffer[j + 1], temp); } - std::swap(buffer[0], buffer[1]); } - auto cost = buffer[0][buffer_length - 1]; - delete [] buffers; + auto cost = buffer[buffer_length - 1]; + delete [] buffer; return cost; } \ No newline at end of file