Skip to content

Instantly share code, notes, and snippets.

@nobu
Created April 27, 2016 04:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobu/beb2d052d56f2029722492dbe7100128 to your computer and use it in GitHub Desktop.
Save nobu/beb2d052d56f2029722492dbe7100128 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
require 'benchmark/ips'
require 'base64'
require 'digest'
N=10_000
data = Digest::SHA256.digest("")
Benchmark.ips do |x|
x.report("base64") do
N.times do
Base64.encode64(data)
end
end
x.report("urlsafe_base64") do
N.times do
Base64.urlsafe_encode64(data)
end
end
x.report("to_i(32)") do
N.times do
data.unpack("H*")[0].to_i(16).to_s(32)
end
end
x.compare!
end
Warming up --------------------------------------
base64 22.000 i/100ms
urlsafe_base64 8.000 i/100ms
to_i(32) 6.000 i/100ms
Calculating -------------------------------------
base64 223.391 (± 5.8%) i/s - 1.122k
urlsafe_base64 84.366 (± 8.3%) i/s - 424.000
to_i(32) 61.936 (± 6.5%) i/s - 312.000
Comparison:
base64: 223.4 i/s
urlsafe_base64: 84.4 i/s - 2.65x slower
to_i(32): 61.9 i/s - 3.61x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment