Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hamming Lazy/Eager benchmarks
class Hamming
def self.eager(s1, s2)
s1.chars.take(10).
zip(s2.chars.take(10)).
count { |c1, c2| c1 != c2 }
end
def self.half_lazy_args(s1, s2)
s1.each_char.take(10).
zip(s2.each_char.take(10)).
count { |c1, c2| c1 != c2 }
end
def self.full_lazy_end(s1, s2)
s1.each_char.lazy.
zip(s2.each_char.lazy).
take(10).
count { |c1, c2| c1 != c2 }
end
def self.full_lazy_args(s1, s2)
s1.each_char.lazy.take(10).
zip(s2.each_char.lazy.take(10)).
count { |c1, c2| c1 != c2 }
end
end
require 'benchmark'
puts
puts "=== TIME - Take 10 - Short strings (25 chars) ==="
puts
short1 = "#{"A" * 23}TG"
short2 = "#{"A" * 23}CT"
Benchmark.bm do |x|
x.report("Eager ") { 100_000.times { Hamming.eager(short1, short2) } }
x.report("Half Lazy - take at args") { 100_000.times { Hamming.half_lazy_args(short1, short2) } }
x.report("Full Lazy - take at end ") { 100_000.times { Hamming.full_lazy_end(short1, short2) } }
x.report("Full Lazy - take at args") { 100_000.times { Hamming.full_lazy_args(short1, short2) } }
end
puts
puts "=== TIME - Take 10 - Medium strings (250 chars) ==="
puts
med1 = "#{"A" * 498}TG"
med2 = "#{"A" * 498}CT"
Benchmark.bm do |x|
x.report("Eager ") { 100_000.times { Hamming.eager(med1, med2) } }
x.report("Half Lazy - take at args") { 100_000.times { Hamming.half_lazy_args(med1, med2) } }
x.report("Full Lazy - take at end ") { 100_000.times { Hamming.full_lazy_end(med1, med2) } }
x.report("Full Lazy - take at args") { 100_000.times { Hamming.full_lazy_args(med1, med2) } }
end
puts
puts "=== TIME - Take 10 - Long strings (10K chars) ==="
puts
long1 = "#{"A" * 9_998}TG"
long2 = "#{"A" * 9_998}CT"
Benchmark.bm do |x|
x.report("Eager ") { 100_000.times { Hamming.eager(long1, long2) } }
x.report("Half Lazy - take at args") { 100_000.times { Hamming.half_lazy_args(long1, long2) } }
x.report("Full Lazy - take at end ") { 100_000.times { Hamming.full_lazy_end(long1, long2) } }
x.report("Full Lazy - take at args") { 100_000.times { Hamming.full_lazy_args(long1, long2) } }
end
=== TIME - Take 10 - Short strings (25 chars) ===
user system total real
Eager 0.687547 0.000525 0.688072 ( 0.688118)
Half Lazy - take at args 0.575680 0.000767 0.576447 ( 0.576753)
Full Lazy - take at end 4.327033 0.037832 4.364865 ( 4.369408)
Full Lazy - take at args 5.685882 0.043873 5.729755 ( 5.733609)
=== TIME - Take 10 - Medium strings (250 chars) ===
user system total real
Eager 8.698948 0.011237 8.710185 ( 8.715160)
Half Lazy - take at args 0.587816 0.000877 0.588693 ( 0.588997)
Full Lazy - take at end 4.180310 0.041642 4.221952 ( 4.224151)
Full Lazy - take at args 5.217934 0.037013 5.254947 ( 5.255484)
=== TIME - Take 10 - Long strings (10K chars) ===
user system total real
Eager 180.554754 0.177740 180.732494 (180.863226)
Half Lazy - take at args 0.597948 0.001913 0.599861 ( 0.601089)
Full Lazy - take at end 4.296089 0.061160 4.357249 ( 4.360040)
Full Lazy - take at args 5.473702 0.068389 5.542091 ( 5.549115)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment