David Alan Black <dblack / candle.superlink.net> writes: > On Thu, 9 Nov 2000, Dave Thomas wrote: > > def common4(w1, w2) > > c = 0 > > w1.each_with_index { |ch, i| c += 1 if w2[i] == ch } > > c > > end > > But doesn't that just split (inclusively) on $/, rather than > interating through the characters? If only $/ could be a regular > expression.... but then it would perhaps only do as well as: Yup - I'm totally wrong on that one. Also, I just tried it with String#each_byte and it was slower than the others. It occurs to me that a speedup might be possible in the Jotto case because one of the words is always fixed (for each side of the game) so you could create a counter object and just pass it the word to be checked: class Common5 def initialize(word) @word = word @len = word.length end def count(word) i = 0 count = 0 @len.times {|i| count += 1 if word[i] == @word[i] } count end end c = Common5.new("hello dave") bm(10) {|x| x.report { 10000.times { common1("hello dave", "wombo dxve") } } x.report { 10000.times { common2("hello dave", "wombo dxve") } } x.report { 10000.times { common3("hello dave", "wombo dxve") } } x.report { 10000.times { common4("hello dave", "wombo dxve") } } x.report { 10000.times { c.count("wombo dxve") } } } For me, that runs as fast as the fastest function-based test. I'm sure there's a clever algorithm that could be implemented by pre-calculating stuff in Common5, much as Boyer-Moore speeds up string searching by pre-computing gaps. However, getting a clever algorithm would require me to be either lucky or clever, and I'm neither today. Regards Dave