Last active
February 8, 2020 05:32
-
-
Save jaredmoody/521fd0e33bbcc690c2de16f6cde6e696 to your computer and use it in GitHub Desktop.
Array of Months Performance
This file contains hidden or 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' | |
start_date = 1.year.ago.to_date | |
end_date = Date.today | |
Benchmark.ips do |x| | |
x.report "map range with Date.new and strftime" do | |
date_range = start_date..end_date | |
date_months = date_range.map {|d| Date.new(d.year, d.month, 1) }.uniq | |
date_months.map {|d| d.strftime "%d/%m/%Y" } | |
end | |
x.report "map range with beginning_of_month and to_s" do | |
(start_date.to_date..end_date.to_date).map(&:beginning_of_month).uniq.map(&:to_s) | |
end | |
x.report "map range once with Date.new and strftime" do | |
(start_date..end_date).map {|d| Date.new(d.year, d.month, 1).strftime "%d/%m/%Y" }.uniq | |
end | |
x.report "map range once with beginning_of_month and to_s" do | |
(start_date.to_date..end_date.to_date).map {|d| d.beginning_of_month.to_s }.uniq | |
end | |
x.report "month increment loop" do | |
dates = [] | |
date = start_date.beginning_of_month | |
while date <= end_date.beginning_of_month do | |
dates << date.to_date.to_s | |
date += 1.month | |
end | |
dates | |
end | |
x.compare! | |
end | |
# Warming up -------------------------------------- | |
# map range with Date.new and strftime | |
# 201.000 i/100ms | |
# map range with beginning_of_month and to_s | |
# 149.000 i/100ms | |
# map range once with Date.new and strftime | |
# 184.000 i/100ms | |
# map range once with beginning_of_month and to_s | |
# 147.000 i/100ms | |
# month increment loop 1.789k i/100ms | |
# Calculating ------------------------------------- | |
# map range with Date.new and strftime | |
# 2.140k (±17.5%) i/s - 10.452k in 5.027441s | |
# map range with beginning_of_month and to_s | |
# 1.552k (±13.2%) i/s - 7.748k in 5.074306s | |
# map range once with Date.new and strftime | |
# 1.823k (± 3.7%) i/s - 9.200k in 5.054044s | |
# map range once with beginning_of_month and to_s | |
# 1.444k (± 6.9%) i/s - 7.203k in 5.011823s | |
# month increment loop 17.788k (± 5.1%) i/s - 89.450k in 5.042600s | |
# | |
# Comparison: | |
# month increment loop: 17788.3 i/s | |
# map range with Date.new and strftime: 2140.1 i/s - 8.31x slower | |
# map range once with Date.new and strftime: 1822.6 i/s - 9.76x slower | |
# map range with beginning_of_month and to_s: 1552.1 i/s - 11.46x slower | |
# map range once with beginning_of_month and to_s: 1443.6 i/s - 12.32x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment