JuliaText / WordNet.jl

A Julia package for Princeton's WordNet®.
Other
35 stars 11 forks source link

Improve performance of `hypernyms()` #16

Closed rdeits closed 5 years ago

rdeits commented 5 years ago

(This PR is based off of #12 )

The current implementation of hypernyms() allocates an array to hold the output of relation() but only takes the first element. It's quite a bit faster to just loop over the synset's pointers and return the first match. For example:

Setup:

julia> using WordNet, BenchmarkTools

julia> db = DB()
WordNet.DB

julia> s = synsets(db, db['n', "lemonade"])
1-element Array{Synset,1}:
 (n) lemonade (sweetened beverage of diluted lemon juice)

Before:

julia> @btime hypernyms($db, $s)
  235.156 ns (6 allocations: 304 bytes)

After:

julia> @btime hypernyms($db, $s)
  30.094 ns (0 allocations: 0 bytes)

There's probably a way to do this in one line with Base.Iterators, but the for loop is the fastest implementation I've found so far, and it doesn't allocate at all.

oxinabox commented 5 years ago

LGTM