Permalink
Browse files

initial

  • Loading branch information...
0 parents commit 93a721c62f49aafd12e00f2b94d767e577dd42bd @nashby committed Apr 7, 2012
Showing with 33 additions and 0 deletions.
  1. +1 −0 Gemfile
  2. +9 −0 Gemfile.lock
  3. +23 −0 rabin-karp.rb
@@ -0,0 +1 @@
+gem 'cityhash'
@@ -0,0 +1,9 @@
+GEM
+ specs:
+ cityhash (0.6.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ cityhash
@@ -0,0 +1,23 @@
+require 'cityhash'
+require 'benchmark'
+
+class String
+ def rk_search(substring)
+ substring_hash = CityHash.hash64(substring)
+ substring_size = substring.size
+
+ each_byte.with_index do |_, i|
+ return if i + substring_size > size
+ return i if CityHash.hash64(self[i, substring_size]) == substring_hash
+ end
+ end
+end
+
+n = 50000
+string = 'hello world'
+substring = 'world'
+
+Benchmark.bm do |x|
+ x.report { n.times { string.rk_search('world') } }
+ x.report { n.times { string.scan('world') } }
+end

0 comments on commit 93a721c

Please sign in to comment.