Skip to content

Instantly share code, notes, and snippets.

@holysugar
Created May 20, 2011 05:06
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 holysugar/982394 to your computer and use it in GitHub Desktop.
Save holysugar/982394 to your computer and use it in GitHub Desktop.
randomstring sample
#!/usr/bin/env ruby
require 'securerandom'
require 'openssl'
require 'base62' # base62 gem
require 'benchmark'
module RandomString
class Concat
def initialize(length = 8)
@length = length
@str=[*('a'..'z'),*('A'..'Z'),*('0'..'9')]
end
def randomstring
"".tap{|result| @length.times{ result << @str[rand(@str.size)] } }
end
end
class Join
def initialize(length = 8)
@length = length
@str=[*('a'..'z'),*('A'..'Z'),*('0'..'9')]
end
def randomstring
Array.new(@length){ @str[rand(@str.size)] }.join
end
end
class SecureRandom
def initialize(length = 8)
@length = length
@randomlength = (length/4.0).ceil * 3
end
def randomstring
::SecureRandom.base64(@randomlength)
end
end
class OpenSSL
def initialize(length = 8)
@length = length
@randomlength = (length/4.0).ceil * 3
end
def randomstring
[::OpenSSL::Random.random_bytes(@randomlength)].pack('m')
end
end
class Base62
def initialize(length = 8)
@length = length
@max = 62**length
@min = 62**(length-1)
@size = @max - @min
end
def randomstring(length = 8)
(::Random.rand(@size)+@min).base62_encode
end
end
end
n = 300000
[8,16,32].each do |length|
concat = RandomString::Concat.new(length)
join = RandomString::Join.new(length)
sran = RandomString::SecureRandom.new(length)
openssl = RandomString::OpenSSL.new(length)
b62 = RandomString::Base62.new(length)
puts
puts "sample(#{length})>"
puts "concat => #{concat.randomstring}"
puts "join => #{join.randomstring}"
puts "sran => #{sran.randomstring}"
puts "openssl => #{openssl.randomstring}"
puts "b62 => #{b62.randomstring}"
puts
Benchmark.bm {|x|
x.report("concat "){ n.times{concat.randomstring} }
x.report("join "){ n.times{join.randomstring} }
x.report("sran "){ n.times{sran.randomstring} }
x.report("openssl "){ n.times{openssl.randomstring} }
x.report("b62 "){ n.times{b62.randomstring} }
}
end
=begin
sample(8)>
concat => 9lEhDm4X
join => LroYhrnZ
sran => XJQuK0bJ
openssl => nJFmqu9W
b62 => weI7rNgw
user system total real
concat 1.140000 0.000000 1.140000 ( 1.147652)
join 1.570000 0.030000 1.600000 ( 1.588756)
sran 2.310000 0.000000 2.310000 ( 2.315751)
openssl 1.270000 0.020000 1.290000 ( 1.280817)
b62 1.300000 0.000000 1.300000 ( 1.304127)
sample(16)>
concat => rGmYvuCPyW6RTMVR
join => fcKvLoizHjZaraHk
sran => 93ncqITQzDfwTvAT
openssl => uIRa/vlG4wtziVSg
b62 => BVtZEVmjpuROSyLj
user system total real
concat 2.050000 0.000000 2.050000 ( 2.058186)
join 2.600000 0.040000 2.640000 ( 2.638252)
sran 2.510000 0.010000 2.520000 ( 2.506866)
openssl 1.450000 0.010000 1.460000 ( 1.461524)
b62 5.290000 0.010000 5.300000 ( 5.300914)
sample(32)>
concat => IUyEjRJkl1jJGBBQbsd9OqFlOriMvSlw
join => z9Nka1tRDFsm29bLfsDYFWuM2FCb9gJX
sran => O/LsNKer4LvFo/nknkFz1U1JkLRA6aRx
openssl => BOvctaodQG2hvRr94G9plvWVck9yZkVO
b62 => rdxQ9HabP9m9Z2a1WpC7LcmLSQJNEGWx
user system total real
concat 4.010000 0.010000 4.020000 ( 4.018792)
join 4.580000 0.080000 4.660000 ( 4.658750)
sran 3.020000 0.010000 3.030000 ( 3.021287)
openssl 1.790000 0.010000 1.800000 ( 1.805106)
b62 16.000000 0.050000 16.050000 ( 16.106021)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment