pearson-hashing 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path('../../lib/pearson-hashing', __FILE__)
4
+
5
+ SIZE = 100_000
6
+
7
+ def benchmark(name, &block)
8
+ puts "#{name}.."
9
+ hashes = {}
10
+ SIZE.times do |i|
11
+ hash = block.call(i.to_s)
12
+ hashes[hash] ||= 0
13
+ hashes[hash] = hashes[hash] + 1
14
+ end
15
+
16
+ # analysis
17
+ collisions = hashes.inject(0){|coll,v| coll += (v[1]-1); coll}
18
+ puts " => #{collisions} collisions per #{SIZE} (#{collisions/SIZE.to_f}%)"
19
+ end
20
+
21
+ benchmark 'PearsonHashing#digest8' do |str|
22
+ PearsonHashing.digest8 str
23
+ end
24
+
25
+ benchmark 'PearsonHashing#digest16' do |str|
26
+ PearsonHashing.digest16 str
27
+ end
28
+
29
+ benchmark 'String#hash' do |str|
30
+ str.hash
31
+ end
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'benchmark'
4
+ require File.expand_path('../../lib/pearson-hashing', __FILE__)
5
+
6
+ Benchmark.bm do |b|
7
+ b.report 'PearsonHashing#digest8' do
8
+ 100_000.times do |i|
9
+ PearsonHashing.digest8 i.to_s
10
+ end
11
+ end
12
+
13
+ b.report 'PearsonHashing#digest16' do
14
+ 100_000.times do |i|
15
+ PearsonHashing.digest16 i.to_s
16
+ end
17
+ end
18
+
19
+ b.report 'String#hash' do
20
+ 100_000.times do |i|
21
+ i.to_s.hash
22
+ end
23
+ end
24
+ end
@@ -50,6 +50,6 @@ module PearsonHashing
50
50
  h1 = PearsonHashing.digest(string)
51
51
  string2 = [((string.bytes.first+1)%256)].pack('U*') + string[1,string.size]
52
52
  h2 = PearsonHashing.digest(string2)
53
- h1 * h2
53
+ ("%03d" % h1 + "%03d" % h2).to_i
54
54
  end
55
55
  end
@@ -1,3 +1,3 @@
1
1
  module PearsonHashing
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -9,7 +9,7 @@ describe PearsonHashing do
9
9
 
10
10
  describe '#digest16' do
11
11
  it 'should return 96 for "foo"' do
12
- PearsonHashing.digest16('foo').should == 9504
12
+ PearsonHashing.digest16('foo').should == 96099
13
13
  end
14
14
  end
15
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pearson-hashing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -23,6 +23,8 @@ files:
23
23
  - MIT-LICENSE
24
24
  - README.md
25
25
  - Rakefile
26
+ - benchmark/collisions.rb
27
+ - benchmark/speed.rb
26
28
  - lib/pearson-hashing.rb
27
29
  - lib/pearson-hashing/version.rb
28
30
  - pearson-hashing.gemspec