Skip to content

Instantly share code, notes, and snippets.

@choonkeat
Forked from sferik/first_to_finish.rb
Last active December 18, 2015 08:57
Show Gist options
  • Save choonkeat/0c254b0c2efbc7237a2a to your computer and use it in GitHub Desktop.
Save choonkeat/0c254b0c2efbc7237a2a to your computer and use it in GitHub Desktop.
Calculating -------------------------------------
@choonkeat 1.000 i/100ms
@sferik 1.000 i/100ms
-------------------------------------------------
@choonkeat 0.100 (± 0.0%) i/s - 1.000 in 10.000901s
@sferik 0.099 (± 0.0%) i/s - 1.000 in 10.087707s
Comparison:
@choonkeat: 0.1 i/s
@sferik: 0.1 i/s - 1.01x slower
Calculating -------------------------------------
@choonkeat 429.000 i/100ms
@sferik 1.000 i/100ms
-------------------------------------------------
@choonkeat 4.953k (±18.2%) i/s - 23.595k
@sferik 9.843 (±40.6%) i/s - 45.000
Comparison:
@choonkeat: 4952.6 i/s
@sferik: 9.8 i/s - 503.14x slower
Calculating -------------------------------------
@choonkeat 1.000 i/100ms
@sferik 1.000 i/100ms
-------------------------------------------------
@choonkeat 0.204 (± 0.0%) i/s - 2.000 in 9.804885s
@sferik 0.198 (± 0.0%) i/s - 1.000
Comparison:
@choonkeat: 0.2 i/s
@sferik: 0.2 i/s - 1.03x slower
require "benchmark/ips"
require "bcrypt"
module Enumerable
def first_to_finish_with_queue
queue = Queue.new
threads = collect { |args| Thread.new {
queue << yield(args)
} }
result = queue.pop
threads.each(&:kill)
result
end
def first_to_finish
threads = collect { |args| Thread.new { yield(args) } }
loop until done = threads.detect { |t| !t.alive? }
threads.each(&:kill)
done.value
end
def get_first_result_async
result = nil
threads = map do |args|
Thread.new do
if current_result = yield(args)
result = current_result
(threads - [Thread.current]).each(&:kill) # kill siblings
end
end
end
threads.each(&:join)
result
end
end
COSTS = (10..15).to_a.shuffle
def choonkeat(&block)
COSTS.first_to_finish_with_queue(&block)
end
def sferik(&block)
COSTS.first_to_finish(&block)
end
lambdas = [
lambda {|cost| sleep cost; cost; },
lambda {|cost| cost; },
lambda {|cost| BCrypt::Password.create("secret", :cost => cost) },
]
lambdas.each do |code|
Benchmark.ips do |x|
x.report("@choonkeat") { choonkeat(&code) }
x.report("@sferik") { sferik(&code) }
x.compare!
end
end
Calculating -------------------------------------
@choonkeat 1.000 i/100ms
@sferik 1.000 i/100ms
-------------------------------------------------
@choonkeat 0.100 (± 0.0%) i/s - 1.000 in 10.009000s
@sferik 0.100 (± 0.0%) i/s - 1.000 in 10.025000s
Comparison:
@choonkeat: 0.1 i/s
@sferik: 0.1 i/s - 1.00x slower
Calculating -------------------------------------
@choonkeat 106.000 i/100ms
@sferik 133.000 i/100ms
-------------------------------------------------
@choonkeat 1.422k (± 4.3%) i/s - 7.102k
@sferik 1.427k (± 6.2%) i/s - 7.182k
Comparison:
@sferik: 1427.5 i/s
@choonkeat: 1422.1 i/s - 1.00x slower
Calculating -------------------------------------
@choonkeat 1.000 i/100ms
@sferik 1.000 i/100ms
-------------------------------------------------
@choonkeat 0.964 (± 0.0%) i/s - 5.000
@sferik 0.688 (± 0.0%) i/s - 4.000 in 5.879000s
Comparison:
@choonkeat: 1.0 i/s
@sferik: 0.7 i/s - 1.40x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment