Last active
August 29, 2015 14:16
-
-
Save rmm5t/90124c34d77cdb04fcf7 to your computer and use it in GitHub Desktop.
Hamming performance benchmarks of http://exercism.io/submissions/2bebda57b2214baa8a56dab7f4b233bd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Courtesy @zenspider | |
# http://exercism.io/submissions/b0613bbac84cd95fcad40e6b | |
class Hamming0 | |
def self.compute s1, s2 | |
length = [s1.length, s2.length].min | |
length.times.count { |i| s1[i] != s2[i] } | |
end | |
end | |
# http://exercism.io/submissions/67e0e6341adb4af68e714393866dd935 | |
class Hamming1 | |
def self.compute(strand_a, strand_b) | |
strand_a.chars.zip(strand_b.chars).count do |pair| | |
pair.first != pair.last | |
end | |
end | |
end | |
# http://exercism.io/submissions/4a1b5c39a08c421f8e7dfcac277c53c8 | |
class Hamming2 | |
def self.compute(strand_a, strand_b) | |
[strand_a.chars, strand_b.chars].transpose.count { |a, b| a != b } | |
end | |
end | |
# http://exercism.io/submissions/2bebda57b2214baa8a56dab7f4b233bd | |
class Hamming3 | |
def self.compute(*strands) | |
strands.map(&:chars).transpose.count do |set| | |
set.uniq.length > 1 | |
end | |
end | |
end | |
class Hamming4 | |
def self.compute(*strands) | |
strands.map(&:chars).transpose.count do |set| | |
set.min != set.max | |
end | |
end | |
end | |
############################################################ | |
require 'benchmark' | |
NUCLEIC_ACIDS = %w(A T C G) | |
STRAND_COUNT = 100 | |
STRAND_LENGTH = 1_000 | |
def random_strand | |
STRAND_LENGTH.times.inject("") { |strand, n| strand << NUCLEIC_ACIDS.sample } | |
end | |
strands = [] | |
STRAND_COUNT.times { |n| strands << random_strand } | |
n = 1_000 | |
Benchmark.bm(36) do |x| | |
x.report("2 strands times") { n.times { Hamming0.compute(*strands[0..1]) } } | |
x.report("2 strands zip") { n.times { Hamming1.compute(*strands[0..1]) } } | |
x.report("2 strands transpose") { n.times { Hamming2.compute(*strands[0..1]) } } | |
x.report("2 strands multi+transpose+uniq") { n.times { Hamming3.compute(*strands[0..1]) } } | |
x.report("2 strands multi+transpose+minmax") { n.times { Hamming4.compute(*strands[0..1]) } } | |
end | |
puts | |
n = 100 | |
Benchmark.bm(36) do |x| | |
x.report("#{STRAND_COUNT} strands multi+transpose+uniq") { n.times { Hamming3.compute(*strands) } } | |
x.report("#{STRAND_COUNT} strands multi+transpose+minmax") { n.times { Hamming4.compute(*strands) } } | |
end | |
# >> user system total real | |
# >> 2 strands times 0.290000 0.000000 0.290000 ( 0.287913) | |
# >> 2 strands zip 0.320000 0.000000 0.320000 ( 0.325399) | |
# >> 2 strands transpose 0.280000 0.000000 0.280000 ( 0.283369) | |
# >> 2 strands multi+transpose+uniq 0.900000 0.010000 0.910000 ( 0.910815) | |
# >> 2 strands multi+transpose+minmax 0.780000 0.010000 0.790000 ( 0.782653) | |
# >> | |
# >> user system total real | |
# >> 100 strands multi+transpose+uniq 2.190000 0.010000 2.200000 ( 2.219818) | |
# >> 100 strands multi+transpose+minmax 2.950000 0.020000 2.970000 ( 2.969106) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment