Skip to content

Instantly share code, notes, and snippets.

@kasumi8pon
Last active October 4, 2020 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasumi8pon/a5376b669a08ea8edfc1edf6da3e30b1 to your computer and use it in GitHub Desktop.
Save kasumi8pon/a5376b669a08ea8edfc1edf6da3e30b1 to your computer and use it in GitHub Desktop.
require 'benchmark'
Benchmark.bm do |x|
n = '5000'
s = 'AT' * 2500
x.report {
n = n.to_i
s = s.chars
answer = 0
(0...n - 1).each do |first|
counter = Hash.new(0)
counter[s[first]] += 1
(first + 1...n).each do |last|
counter[s[last]] += 1
if counter['A'] == counter['T'] && counter['C'] == counter['G']
answer += 1
end
end
end
}
n = '5000'
s = 'AT' * 2500
x.report {
n = n.to_i
s = s.chars.map(&:to_sym)
answer = 0
(0...n - 1).each do |first|
counter = Hash.new(0)
counter[s[first]] += 1
(first + 1...n).each do |last|
counter[s[last]] += 1
if counter[:A] == counter[:T] && counter[:C] == counter[:G]
answer += 1
end
end
end
}
end
# ❯ ruby arc/104/b_benchmark.rb
# user system total real
# 3.077603 0.004862 3.082465 ( 3.098469)
# 1.767028 0.002098 1.769126 ( 1.774524)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment