Skip to content

Instantly share code, notes, and snippets.

@wtn
Created April 5, 2011 20:33
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 wtn/904482 to your computer and use it in GitHub Desktop.
Save wtn/904482 to your computer and use it in GitHub Desktop.
modify array with << and delete instead of + and -
ATTRIBUTES = %w{a b c d e f g h i j _id}
t0 = Time.now
100_000.times do
ATTRIBUTES.dup.map { |k| k.to_s } + ['id'] - ['_id']
end
map_time = Time.now - t0
t1 = Time.now
100_000.times do
acc = []
ATTRIBUTES.dup.each { |k| acc << k.to_s }
acc << 'id'
acc.delete('_id')
acc
end
each_time = Time.now - t1
savings_ratio = (map_time - each_time).to_f / map_time
puts "You saved #{"%.2f" % (savings_ratio * 100)}%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment