Skip to content

Instantly share code, notes, and snippets.

@halida
Created April 3, 2012 07:45
Show Gist options
  • Save halida/2290229 to your computer and use it in GitHub Desktop.
Save halida/2290229 to your computer and use it in GitHub Desktop.
test map speed
def timeit name
begins = Time.now
yield
ends = Time.now
puts "running: #{name}, spend time: #{ends - begins}"
end
class MyData
def initialize(value)
@value = value
end
def value
@value
end
end
def process_with_map objs
out = objs.map{|i| i.value}
end
def process_with_amp objs
out = objs.map(&:value)
end
datas = 1000.times.map{|i| MyData.new i }
timeit "process with map" do
1000.times{ process_with_map(datas) }
end
timeit "process with amp" do
1000.times{ process_with_amp(datas) }
end
@halida
Copy link
Author

halida commented Apr 3, 2012

result:

 $ ruby test.rb
running: process with map, spend time: 0.367840264
running: process with amp, spend time: 0.323653619

@zavakid
Copy link

zavakid commented Apr 3, 2012

this is my result:

running: process with map, spend time: 0.226932842
running: process with amp, spend time: 0.246727534

seems mine is more natural.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment