-
-
Save matthewd/6827623f4e0308943e8c82db490cc118 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
require 'benchmark/ips' | |
require 'securerandom' | |
n = 50 | |
alphabet = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a - ['0', 'O', 'I', 'l'] | |
alphabet2 = ('0'..'9').to_a + ('a'..'z').to_a | |
Benchmark.ips do |x| | |
x.report("clever58") do | |
SecureRandom.random_bytes(n).unpack("C*").map do |byte| | |
idx = byte % 64 | |
idx = SecureRandom.random_number(58) if idx >= 58 | |
alphabet[idx] | |
end.join | |
end | |
x.report("pedestrian58") do | |
n.times.map { alphabet[SecureRandom.random_number(58)] }.join | |
end | |
x.report("suggested36") do | |
SecureRandom.random_bytes(n).unpack("C*").map do |byte| | |
idx = byte / 7 | |
idx = SecureRandom.random_number(36) if idx >= 36 | |
alphabet2[idx] | |
end.join | |
end | |
x.report("clever36") do | |
SecureRandom.random_bytes(n).unpack("C*").map do |byte| | |
idx = byte % 64 | |
idx = SecureRandom.random_number(36) if idx >= 36 | |
alphabet2[idx] | |
end.join | |
end | |
x.report("pedestrian36") do | |
n.times.map { alphabet2[SecureRandom.random_number(36)] }.join | |
end | |
end |
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 base58.rb | |
Warming up -------------------------------------- | |
clever58 1.004k i/100ms | |
pedestrian58 148.000 i/100ms | |
suggested36 2.653k i/100ms | |
clever36 283.000 i/100ms | |
pedestrian36 144.000 i/100ms | |
Calculating ------------------------------------- | |
clever58 11.291k (± 4.9%) i/s - 57.228k in 5.081750s | |
pedestrian58 1.447k (± 5.5%) i/s - 7.252k in 5.027963s | |
suggested36 28.269k (± 4.0%) i/s - 143.262k in 5.076386s | |
clever36 3.072k (± 3.9%) i/s - 15.565k in 5.075289s | |
pedestrian36 1.441k (± 2.6%) i/s - 7.200k in 5.000994s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment