Created
November 8, 2018 20:57
-
-
Save GustavoCaso/999808de80726442d46db1ad18414dea to your computer and use it in GitHub Desktop.
building min max array
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 'bundler/inline' | |
require 'benchmark' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'pry' | |
end | |
LAST_ITEM_SCORE = 1573263909 | |
THREE_WEEKS_FROM_NOW = 1543521400 | |
RANGE_INCREMENT_BY = 100_000 | |
def using_ranges | |
(THREE_WEEKS_FROM_NOW..LAST_ITEM_SCORE).to_a.each_slice(RANGE_INCREMENT_BY).to_a.map! { |slice| [slice[0], slice[-1]] } | |
end | |
def custom_iteration | |
number_of_ranges = (LAST_ITEM_SCORE - THREE_WEEKS_FROM_NOW) / RANGE_INCREMENT_BY | |
ranges = [[THREE_WEEKS_FROM_NOW, (THREE_WEEKS_FROM_NOW + RANGE_INCREMENT_BY)]] | |
number_of_ranges.times do | |
ranges << [ranges.last[-1], (ranges.last[-1] + RANGE_INCREMENT_BY)] | |
end | |
ranges | |
end | |
Benchmark.bmbm(28) do |x| | |
x.report('using_ranges:') { using_ranges } | |
x.report('custom_iteration') { custom_iteration } | |
end | |
puts "The same number of steps: #{ using_ranges.size == custom_iteration.size }" |
Author
GustavoCaso
commented
Nov 8, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment