Skip to content

Instantly share code, notes, and snippets.

@max-did-it
Created May 26, 2020 17:10
Show Gist options
  • Save max-did-it/e721c4e590305e2489264bfe47ccfbd6 to your computer and use it in GitHub Desktop.
Save max-did-it/e721c4e590305e2489264bfe47ccfbd6 to your computer and use it in GitHub Desktop.
bm
require 'benchmark'
require "benchmark/memory"
def func()
target, *arr = IO.readlines('input.txt', ' ')
target, first_el = target.split("\n")
arr.unshift(first_el)
target = target.to_i
while el = arr.shift
delta = target - el.to_i
if arr.bsearch { |e| !!e.match(%r{#{delta.to_s}(\n)?}) }
puts '1'
break
end
end
end
def generate_input(try)
a = (try+1)**3
target = 10
nums = (0..a).map { |_| rand(1..3)}.join(" ") + " 5 5"
File.open('input.txt', 'w') do |f|
f.write(target.to_s + "\n")
f.write(nums)
end
end
40.times do |t|
generate_input(t)
Benchmark.bm(10) do |bm|
bm.report("TRY N-#{t}| COUNT ELEMENTS #{((t+1)**3)+2} | FILE input.txt SIZE: #{File.size('input.txt')}") do
func()
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment