Create a gist now

Instantly share code, notes, and snippets.

require 'benchmark'
namespace :benchmark do
task :test => :environment do
# Repeat these values 100 times
values = [[
{:timestamp => "2011-01-01 00:00", :value => 1},
{:timestamp => "2011-01-01 00:15", :value => 2},
{:timestamp => "2011-01-01 00:30", :value => 3}
],[
{:timestamp => "2011-01-01 00:00", :value => 1},
{:timestamp => "2011-01-01 00:30", :value => 3}
],[
{:timestamp => "2011-01-01 00:00", :value => 1},
{:timestamp => "2011-01-01 00:15", :value => 2},
{:timestamp => "2011-01-01 00:30", :value => 3}
]].map{|x|[x]*100}.flatten(1)
Benchmark.bmbm do |x|
x.report("test 1") do
10_000.times do
all_stamps = values.flatten.map{|x| x[:timestamp]}.uniq.sort
values.each do |set|
my_stamps=set.map{|x|x[:timestamp]}.uniq
missing = all_stamps - my_stamps
set.concat( missing.map{ |stamp| {:timestamp => stamp, :value => nil} } )
set.replace( set.sort_by{ |x| x[:timestamp] } )
end
end
end
x.report("test2") do
10_000.times do
stamps = values.map{ |logs| logs.map{ |row| row[:timestamp] } }.flatten.uniq.sort
values.map!{ |logs| stamps.map { |ts| logs.select{ |row| row[:timestamp] == ts }.first || { :timestamp => ts, :value => nil } } }
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment