Skip to content

Commit

Permalink
ext/digest: faster code. (merged from CRuby)
Browse files Browse the repository at this point in the history
ruby/ruby@69f1e595

Test Script:
----
require 'benchmark'

Benchmark.bm(10) do |x|
  x.report "before" do
    10000.times do
      ipad = Array.new(1000).fill(0x36)
      ipad.inject('') { |s, c| s << c.chr }.freeze
    end
  end

  x.report "after" do
    10000.times do
      ipad = Array.new(1000, 0x36)
      ipad.pack('C*').freeze
    end
  end
end
----
                user     system      total        real
before     15.220000   0.320000  15.540000 ( 13.063469)
after       0.460000   0.010000   0.470000 (  0.453947)
  • Loading branch information
Watson1978 committed Dec 23, 2011
1 parent 34ebcb6 commit 31449b3
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions ext/digest/lib/digest/hmac.rb
Expand Up @@ -49,17 +49,17 @@ def initialize(key, digester)
key = @md.digest(key)
end

ipad = Array.new(block_len).fill(0x36)
opad = Array.new(block_len).fill(0x5c)
ipad = Array.new(block_len, 0x36)
opad = Array.new(block_len, 0x5c)

key.bytes.each_with_index { |c, i|
ipad[i] ^= c
opad[i] ^= c
}

@key = key.freeze
@ipad = ipad.inject('') { |s, c| s << c.chr }.freeze
@opad = opad.inject('') { |s, c| s << c.chr }.freeze
@ipad = ipad.pack('C*').freeze
@opad = opad.pack('C*').freeze
@md.update(@ipad)
end

Expand Down Expand Up @@ -102,7 +102,7 @@ def inspect
end

if $0 == __FILE__
eval DATA.read, nil, $0, __LINE__+4
eval DATA.gets(nil), nil, $0, DATA.lineno
end

__END__
Expand Down Expand Up @@ -142,7 +142,7 @@ def test_reset
}
}
end
end
end

class TC_HMAC_MD5 < Test::Unit::TestCase
include TM_HMAC
Expand Down

0 comments on commit 31449b3

Please sign in to comment.