Ruby 1.8.5 のSHA1 がやたら速いんだが
何故だ。MD5 の方が速いんじゃないの?
ベンチマーク
$ /usr/local/bin/ruby -v hash_bench.rb [~/tmp] ruby 1.8.5 (2006-08-25) [i686-darwin9.5.0] Rehearsal ---------------------------------------- MD5 4.070000 0.010000 4.080000 ( 4.102120) SHA1 1.580000 0.010000 1.590000 ( 1.602017) ------------------------------- total: 5.670000sec user system total real MD5 4.030000 0.020000 4.050000 ( 4.083039) SHA1 1.620000 0.010000 1.630000 ( 1.634931) $ /usr/bin/ruby -v hash_bench.rb [~/tmp] ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] Rehearsal ---------------------------------------- MD5 2.520000 0.010000 2.530000 ( 2.557895) SHA1 3.570000 0.010000 3.580000 ( 3.612239) ------------------------------- total: 6.110000sec user system total real MD5 2.530000 0.010000 2.540000 ( 2.551385) SHA1 3.620000 0.020000 3.640000 ( 3.683908) $ /opt/local/bin/ruby -v hash_bench.rb [~/tmp] ruby 1.8.7 (2008-06-20 patchlevel 22) [i686-darwin9] Rehearsal ---------------------------------------- MD5 4.560000 0.840000 5.400000 ( 5.442298) SHA1 4.970000 0.870000 5.840000 ( 5.874196) ------------------------------ total: 11.240000sec user system total real MD5 4.570000 0.840000 5.410000 ( 5.439444) SHA1 4.980000 0.860000 5.840000 ( 5.894651)
ソース
require 'benchmark' require 'digest/sha1' require 'digest/md5' n = 10**6 str = 'Ax098UjkG' Benchmark.bmbm do |x| x.report('MD5 '){ n.times{ Digest::MD5.hexdigest(str)}} x.report('SHA1'){ n.times{ Digest::SHA1.hexdigest(str)}} end
追記
16進数の文字列への変換が問題でした。
ベンチマーク
$ /usr/local/bin/ruby -v hash_bench.rb [~/tmp] ruby 1.8.5 (2006-08-25) [i686-darwin9.5.0] Rehearsal ---------------------------------------- MD5 1.350000 0.010000 1.360000 ( 1.378982) SHA1 1.490000 0.010000 1.500000 ( 1.532837) ------------------------------- total: 2.860000sec user system total real MD5 1.310000 0.000000 1.310000 ( 1.358399) SHA1 1.530000 0.010000 1.540000 ( 1.547941)
ソース
require 'benchmark' require 'digest/sha1' require 'digest/md5' n = 10**6 str = 'Ax098UjkG' Benchmark.bmbm do |x| x.report('MD5 '){ n.times{ Digest::MD5.digest(str)}} x.report('SHA1'){ n.times{ Digest::SHA1.digest(str)}} end