Skip to content

Instantly share code, notes, and snippets.

@glaszig
Last active June 20, 2016 20:49
Show Gist options
  • Save glaszig/9d1975b1821a799c5db5957c4cff1539 to your computer and use it in GitHub Desktop.
Save glaszig/9d1975b1821a799c5db5957c4cff1539 to your computer and use it in GitHub Desktop.
ruby hex encode algorithm performance comparison
require 'benchmark'
ITERATIONS=100_000
INPUT='https://www.example.com/path/to/image.png'.freeze
def interpolate
INPUT.to_enum(:each_byte).map{ |byte| "%02x" % byte }.join
end
def unpack
INPUT.unpack('H*').first
end
Benchmark.bmbm do |b|
b.report 'interpolation' do
ITERATIONS.times { interpolate }
end
b.report 'String#unpack' do
ITERATIONS.times { unpack }
end
end
puts
puts " input: #{INPUT}"
puts "interpolation: #{interpolate}"
puts "String#unpack: #{unpack}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment