Last active
December 20, 2015 16:08
-
-
Save sidazhang/6159031 to your computer and use it in GitHub Desktop.
map vs map to proc benchmark
This file contains 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
require 'benchmark' | |
def map | |
100000.times do | |
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100].map do |int| | |
int.to_s | |
end | |
end | |
end | |
def map_to_proc | |
100000.times do | |
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100].map(&:to_s) | |
end | |
end | |
threads = [] | |
5.times do | |
threads << Thread.new do | |
puts "map: #{Benchmark.measure { map }}" | |
end | |
end | |
5.times do | |
threads << Thread.new do | |
puts "map_to_proc: #{Benchmark.measure { map_to_proc }}" | |
end | |
end | |
threads.each { |thr| thr.join } | |
# results | |
# map: 2.310000 0.000000 2.310000 ( 2.309747) | |
# map: 3.000000 0.010000 3.010000 ( 3.008737) | |
# map: 2.610000 0.010000 2.620000 ( 2.611520) | |
# map_to_proc: 2.390000 0.010000 2.400000 ( 2.389130) | |
# map_to_proc: 3.180000 0.010000 3.190000 ( 3.184652) | |
# map_to_proc: 2.790000 0.010000 2.800000 ( 2.791936) | |
# map_to_proc: 2.780000 0.010000 2.790000 ( 2.787186) | |
# map_to_proc: 2.780000 0.010000 2.790000 ( 2.779340) | |
# map: 3.400000 0.010000 3.410000 ( 3.407253) | |
# map: 3.380000 0.010000 3.390000 ( 3.382457) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment