Created
April 3, 2012 07:45
-
-
Save halida/2290229 to your computer and use it in GitHub Desktop.
test map speed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
result: