Last active
June 20, 2016 20:49
-
-
Save glaszig/9d1975b1821a799c5db5957c4cff1539 to your computer and use it in GitHub Desktop.
ruby hex encode algorithm performance comparison
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' | |
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