Skip to content

Instantly share code, notes, and snippets.

@GustavoCaso
Created November 8, 2018 20:57
Show Gist options
  • Save GustavoCaso/999808de80726442d46db1ad18414dea to your computer and use it in GitHub Desktop.
Save GustavoCaso/999808de80726442d46db1ad18414dea to your computer and use it in GitHub Desktop.
building min max array
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 }"
@GustavoCaso
Copy link
Author

Rehearsal ----------------------------------------------------------------
using_ranges:                  1.379709   0.151583   1.531292 (  1.533305)
custom_iteration               0.000042   0.000001   0.000043 (  0.000042)
------------------------------------------------------- total: 1.531335sec

                                   user     system      total        real
using_ranges:                  1.238085   0.119271   1.357356 (  1.360325)
custom_iteration               0.000066   0.000002   0.000068 (  0.000064)
The same number of stpes: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment