Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@floere
Created October 26, 2011 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floere/1315618 to your computer and use it in GitHub Desktop.
Save floere/1315618 to your computer and use it in GitHub Desktop.
Running Picky e.g. in a single script.
# Possible since Picky 3.2.0.
#
require 'picky'
include Picky
Thing = Struct.new :id, :name
index = Index.new :test do
category :name, similarity: Similarity::DoubleMetaphone.new(3)
end
index.replace Thing.new(1, 'Picky')
index.replace Thing.new(2, 'Parslet')
things = Search.new(index) do
boost [:name] => +3
end
p things.search("Pick").ids
p things.search("Pic").to_hash
p things.search("Parsley~").allocations
@floere
Copy link
Author

floere commented Oct 27, 2011

# Or, replace the three last lines with:
#
require 'benchmark'

p Benchmark.measure { 1000.times { things.search("Picky") } }    # Exact, ... (  0.112894)
p Benchmark.measure { 1000.times { things.search("Pic") } }      # Partial, ... (  0.118742)
p Benchmark.measure { 1000.times { things.search("Parsley~") } } # Similar, ... (  0.203989)

@floere
Copy link
Author

floere commented Oct 27, 2011

# Or even, replace them with.
#
class Proxy

  def initialize things
    @things = things
  end

  def search text
    @things.search(text).to_hash
  end

end

require 'msgpack/rpc'
svr = MessagePack::RPC::Server.new
svr.listen '127.0.0.1', 18800, Proxy.new(things)
svr.run

# Run it, and then open another file with
#
require 'msgpack/rpc'
c = MessagePack::RPC::Client.new '127.0.0.1', 18800

require 'benchmark'
p Benchmark.realtime { 1000.times { c.call :search, "Picky" } } # => 0.231717

# And run that :)
#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment