Skip to content

Instantly share code, notes, and snippets.

@fffx
Last active February 1, 2018 01:56
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 fffx/2f6fd2d6a8c48362c3dfab99afc5a441 to your computer and use it in GitHub Desktop.
Save fffx/2f6fd2d6a8c48362c3dfab99afc5a441 to your computer and use it in GitHub Desktop.
a optimized way count weekend days
#!/usr/bin/env ruby
require 'date'
require 'benchmark'
def weekend_count(start_date, end_date)
size = (end_date - start_date).to_i
count = 0
if start_date.wday != 0
size -= (7 - start_date.wday).to_i
count += 1
end
left_over = size % 7
if left_over == 0
count = (count / 7) * 2
else
size -= left_over
count += (size / 7) * 2 + 1
end
count
end
def weekend_count_normal(start_date, end_date)
(start_date..end_date).count{ |d| [0, 6].include?(d.wday) }
end
distances = [10, 100, 1000, 10000, 100000]
today = Date.today
entries = distances.map { |d| [today - d, today] }
entries.each do |entry|
puts "Distance #{(entry[1] - entry[0]).to_i}"
puts "Optimized result: #{weekend_count(entry[0], entry[1])}"
puts "Normal result: #{weekend_count_normal(entry[0], entry[1])}"
Benchmark.bm(100) do |bm|
bm.report('Optimized') do
weekend_count(entry[0], entry[1])
end
bm.report('Normal') do
weekend_count_normal(entry[0], entry[1])
end
end
en
@fffx
Copy link
Author

fffx commented Dec 12, 2017

benchmark result

Distance 10
Optimized result:  4
Normal result:     4
                                                  user     system      total        real
Optimized                                     0.000000   0.000000   0.000000 (  0.000018)
Normal                                        0.000000   0.000000   0.000000 (  0.000032)
Distance 100
Optimized result:  29
Normal result:     29
                                                  user     system      total        real
Optimized                                     0.000000   0.000000   0.000000 (  0.000006)
Normal                                        0.000000   0.000000   0.000000 (  0.000094)
Distance 1000
Optimized result:  286
Normal result:     286
                                                  user     system      total        real
Optimized                                     0.000000   0.000000   0.000000 (  0.000010)
Normal                                        0.000000   0.000000   0.000000 (  0.000650)
Distance 10000
Optimized result:  2858
Normal result:     2858
                                                  user     system      total        real
Optimized                                     0.000000   0.000000   0.000000 (  0.000013)
Normal                                        0.000000   0.000000   0.000000 (  0.004995)
Distance 100000
Optimized result:  28572
Normal result:     28572
                                                  user     system      total        real
Optimized                                     0.000000   0.000000   0.000000 (  0.000011)
Normal                                        0.060000   0.000000   0.060000 (  0.064223)

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