Created
June 14, 2011 11:18
-
-
Save andruby/1024707 to your computer and use it in GitHub Desktop.
Ruby 1.9.0 group_by benchmark
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 'rubygems' | |
require 'activesupport' | |
require 'benchmark' | |
include Benchmark | |
class T | |
attr_accessor :date, :count | |
def initialize(_date,_count) | |
@count = _count | |
@date = _date | |
end | |
end | |
def fill_array(n) | |
a = [] | |
random_dates = n / 5 | |
n.times { |x| a << T.new(rand(random_dates).days.ago,x) } | |
return a | |
end | |
(2..5).each do |p| | |
n = 10**p | |
puts "=== 10^#{p} ===" | |
array = [] | |
benchmark do |x| | |
x.report("filling array : ") { array = fill_array(n) } | |
x.report("group_by (Time) : ") { array.group_by { |r| r.date } } | |
x.report("group_by (Date) : ") { array.group_by { |r| r.date.to_date } } | |
x.report("group_by (String) : ") { array.group_by { |r| r.date.to_date.to_s } } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment