class BCrypt::Engine

def self.calibrate(upper_time_limit_in_ms)

BCrypt::Password.create("woo", :cost => 12)
# should take less than 1000ms

BCrypt::Password.create("woo", :cost => 10)
# should take less than 200ms

BCrypt::Engine.calibrate(1000) #=> 12
BCrypt::Engine.calibrate(200) #=> 10

Example:

Returns the cost factor which will result in computation times less than +upper_time_limit_in_ms+.
def self.calibrate(upper_time_limit_in_ms)
  (BCrypt::Engine::MIN_COST..BCrypt::Engine::MAX_COST-1).each do |i|
    start_time = Time.now
    Password.create("testing testing", :cost => i+1)
    end_time = Time.now - start_time
    return i if end_time * 1_000 > upper_time_limit_in_ms
  end
end