Skip to content

Instantly share code, notes, and snippets.

@beerlington
Created January 26, 2011 13:18
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 beerlington/03ea4920421986700257 to your computer and use it in GitHub Desktop.
Save beerlington/03ea4920421986700257 to your computer and use it in GitHub Desktop.
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