secret
Created

  • Download Gist
benchmark.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.