Skip to content

Instantly share code, notes, and snippets.

@manveru
Created February 1, 2013 07:41
Show Gist options
  • Save manveru/4689958 to your computer and use it in GitHub Desktop.
Save manveru/4689958 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'digest/sha2'
java_import 'java.security.MessageDigest'
def jhex(s)
md = MessageDigest.getInstance("SHA-256")
md.update(s.to_java(:String).getBytes)
md.digest.to_a.pack('C*').unpack('H*')
end
def rhex(s)
Digest::SHA256.hexdigest(s)
end
srand 42
chars = [*"a".."z", *"A".."Z"]
test = Array.new(200000){
Array.new(rand(1..2000)){ chars.sample }.join
}
Benchmark.bmbm 20 do |b|
b.report 'java' do
test.each{|t| jhex(t) }
end
b.report 'ruby' do
test.each{|t| rhex(t) }
end
end
Rehearsal --------------------------------------------------------
java 6.860000 0.080000 6.940000 ( 4.604000)
ruby 2.040000 0.000000 2.040000 ( 1.794000)
----------------------------------------------- total: 8.980000sec
user system total real
java 4.790000 0.050000 4.840000 ( 3.883000)
ruby 1.620000 0.000000 1.620000 ( 1.609000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment