Comments (5)
As an example
words1 = [join(rand('a':'z', rand(4:10))) for _ in 1:100]
words2 = [join(rand('a':'z', rand(4:10))) for _ in 1:100]
function f1(w1, w2)
dl = DamerauLevenshtein()
for word1 in w1, word2 in w2
evaluate(dl, word1, word2)
end
end
function f2(w1, w2)
dl = DamerauLevenshtein(10)
for word1 in w1, word2 in w2
evaluate!(dl, word1, word2)
end
end
@benchmark f1($words1, $words2)
BenchmarkTools.Trial:
memory estimate: 2.79 MiB
allocs estimate: 20000
--------------
minimum time: 2.785 ms (0.00% GC)
median time: 3.038 ms (0.00% GC)
mean time: 3.131 ms (2.09% GC)
maximum time: 5.036 ms (24.49% GC)
--------------
samples: 1596
evals/sample: 1
@benchmark f2($words1, $words2)
BenchmarkTools.Trial:
memory estimate: 352 bytes
allocs estimate: 3
--------------
minimum time: 2.006 ms (0.00% GC)
median time: 2.170 ms (0.00% GC)
mean time: 2.188 ms (0.00% GC)
maximum time: 3.021 ms (0.00% GC)
--------------
samples: 2283
evals/sample: 1
from stringdistances.jl.
Interesting. If I understand correctly, DamarauLevenshtein(10)
creates a vector v0
of length 10 that gets reused over time? What I don't like is that there is a limit on the length of the vector. Could you check when v0
is a dictionary?
from stringdistances.jl.
The other possibility would be to create a vector of length 30 by default, say, and then push!
to it when necessary to increase its length.
from stringdistances.jl.
So it looks like it should not be difficult to add this. The cost is that (i) it makes the code a bit more complicated (ii) it requires to create the distance outside the loop (iii) it may complicate stuff like multithreading. I'm not sure it outweighs the benefits. Are there cases where it's really important to avoid these allocations?
from stringdistances.jl.
10 was only an example, basically a sizehint!
to preallocate state vectors, of course, they can be resized on the fly. As for the multithreading and other valid points, the idea is to have both versions of the evaluate
function: current immutable and additionally non-thread safe mutable evalute!
. It will not break the current code just give additional options for those who can use it.
It will add small overhead, which I think is negligible compared to the cost of evaluate
function itself.
mutable struct DL
v0::Vector{Int}
v2::Vector{Int}
DL(sizehint) = new(Vector{Int}(undef, sizehint), Vector{Int}(undef, sizehint))
DL() = new()
end
@btime DL()
4.560 ns (1 allocation: 32 bytes)
# as compared to current implementation
struct DL2 end
@btime DL2()
0.017 ns (0 allocations: 0 bytes)
I've stumbled upon this, when I was implementing SymSpell fuzzy search, at some point I had to compare original phrase with lots of suggestions using DL algorithm and mutating version increased speed significantly. And overall changes are minimal, it's basically these three lines: https://github.com/Arkoniak/SymSpellChecker.jl/blob/master/src/distance.jl#L34-L36
from stringdistances.jl.
Related Issues (20)
- Phonetic distance HOT 1
- Tag a new version HOT 1
- `Base.findmin(s1, s2, dist::Partial)`
- bug in `DamerauLevenshtein` HOT 9
- `compare` with `Partial` distances gives negative answers HOT 4
- DamerauLevenshtein() vs Levenshtein() why the same distance ? HOT 1
- Speeding up qgram distances with pre-counting of qgrams HOT 9
- (Partial) Hamming distance HOT 5
- TagBot trigger issue HOT 5
- Simpler QGramDistances implementation and prep for general dictionaries and iterators HOT 5
- `Partial` only looks at substrings of the same length... HOT 1
- pairwise not working with StringDistances HOT 3
- unexpected behavior when computing distance with an array HOT 2
- Non-strings HOT 4
- The value of "compare" is probably wrong. HOT 1
- Feature Request: Parallel processing HOT 4
- incremental compilation may be fatally broken for this module HOT 5
- Julia v1.7 Jaro() doesn't work HOT 2
- incomplete readme documentation HOT 1
- NaN (or ArgumentError) from QGram distances for short strings HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stringdistances.jl.