Last active
August 29, 2015 14:20
-
-
Save l3kn/3b0e41142a6532b7b0c2 to your computer and use it in GitHub Desktop.
Exercism - Hamming Benchmark
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
require 'benchmark/ips' | |
require 'ruby-prof' | |
seq1 = 'GAGCCTACTAACGGGAT' | |
seq2 = 'CATCGTAATGACGGCCT' | |
module Allocation | |
def self.count | |
GC.disable | |
before = ObjectSpace.count_objects | |
yield | |
after = ObjectSpace.count_objects | |
GC.enable | |
after.keys.each do |key| | |
diff = after[key] - (before[key] || 0) | |
puts " #{key}: #{diff}" if diff != 0 | |
end | |
end | |
end | |
class Hamming | |
def self.compute1(seq1, seq2) | |
seq1.chars.each_with_index.select { |char, i| char != seq2[i] }.count | |
end | |
def self.compute2(seq1, seq2) | |
seq1.each_char.with_index.select { |char, i| char != seq2[i] }.count | |
end | |
def self.compute3(seq1, seq2) | |
seq1.chars.each_with_index.count { |char, i| char != seq2[i] } | |
end | |
def self.compute4(seq1, seq2) | |
seq1.each_char.with_index.count { |char, i| char != seq2[i] } | |
end | |
end | |
functions = [:compute1, :compute2, :compute3, :compute4] | |
Benchmark.ips do |x| | |
x.config(:time => 0.5, :warmup => 0.1) | |
functions.each do |function| | |
x.report(function.to_s) do | |
Hamming.send(function, seq1, seq2) | |
end | |
end | |
end | |
n = 100_000 | |
functions.each do |function| | |
puts function | |
Allocation.count do | |
n.times do | |
Hamming.send(function, seq1, seq2) | |
end | |
end | |
end |
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
Calculating ------------------------------------- | |
compute1 8.077k i/100ms | |
compute2 8.304k i/100ms | |
compute3 10.468k i/100ms | |
compute4 11.110k i/100ms | |
------------------------------------------------- | |
compute1 102.634k (± 0.6%) i/s - 56.539k in 0.550900s | |
compute2 105.863k (± 0.5%) i/s - 58.128k in 0.549102s | |
compute3 141.892k (± 0.5%) i/s - 73.276k | |
compute4 153.960k (± 0.5%) i/s - 77.770k | |
compute1 | |
TOTAL: 5867806 | |
FREE: -32223 | |
T_STRING: 3500027 | |
T_ARRAY: 1900001 | |
T_HASH: 1 | |
T_DATA: 100000 | |
T_NODE: 400000 | |
compute2 | |
TOTAL: 2323813 | |
FREE: -3576188 | |
T_STRING: 3500000 | |
T_ARRAY: 1800000 | |
T_HASH: 1 | |
T_DATA: 200000 | |
T_NODE: 400000 | |
compute3 | |
FREE: -4200001 | |
T_STRING: 3500000 | |
T_ARRAY: 100000 | |
T_HASH: 1 | |
T_DATA: 100000 | |
T_NODE: 500000 | |
compute4 | |
FREE: -4200001 | |
T_STRING: 3500000 | |
T_HASH: 1 | |
T_DATA: 200000 | |
T_NODE: 500000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment