Created
August 11, 2017 20:11
-
-
Save mahemoff/dc4b738c11ed3fed9e25f02cb1802c38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ruby 2.3.1p112 MacOS on MBP Retina late 2013 | |
# Benchmarking some techniques at: | |
# https://stackoverflow.com/questions/12911869/is-there-a-faster-way-to-generate-random-string-in-ruby | |
# https://stackoverflow.com/questions/88311/how-to-generate-a-random-string-in-ruby | |
# Results show Object.hash is easily the fastest and shuffle is the slowest (as well as not being great as it doesn't support repeating characters) | |
# SecureRandom is a good compromise, especially if the string is needed for security purposes. It's 5 tims slower than Object.hash, but Object.hash | |
# is an int that would need converting to string to make an equivalent, smaller, representation. | |
[35] pry(main)> all=[]; Benchmark.measure { 10000.times { all << ('a'..'z').to_a.shuffle[0,8].join} } | |
#<Benchmark::Tms:0x007fded6ef38e8 @label="", @real=0.11732996697537601, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.1200000000000001, @total=0.1200000000000001> | |
[37] pry(main)> all=[]; Benchmark.measure { 10000.times { all << SecureRandom.urlsafe_base64(8) } } | |
#<Benchmark::Tms:0x007fded5e01a08 @label="", @real=0.03568593499949202, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.040000000000000036, @total=0.040000000000000036> | |
[38] pry(main)> all=[]; Benchmark.measure { 10000.times { all << Object.hash.to_s } } | |
#<Benchmark::Tms:0x007fded58a2cd8 @label="", @real=0.007540464983321726, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.009999999999999787, @total=0.009999999999999787> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment