Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Enumerator::Lazy#zip for better performance
$ cat bench.rb require "benchmark/ips" a = Array.new(100) { 1 }.freeze b = Array.new(100) { 2 }.freeze Benchmark.ips do |x| x.report('lazy.zip.map') { a.lazy.zip(b).map {|l,r| l + r } } x.report('zip.map') { a.zip(b).map {|l,r| l + r } } x.report('map.with_index') { a.map.with_index {|i,index| i + b[index] } } x.report('each_with_index') do |times| new_array = [] i = 0 while i < times a.each_with_index {|i,index| new_array << i + b[index] } i = i + 1 end end x.compare! end $ ruby bench.rb Warming up -------------------------------------- lazy.zip.map 17.413k i/100ms zip.map 7.960k i/100ms map.with_index 10.779k i/100ms each_with_index 9.897k i/100ms Calculating ------------------------------------- lazy.zip.map 186.025k (± 3.4%) i/s - 940.302k in 5.060879s zip.map 81.511k (± 4.0%) i/s - 413.920k in 5.087448s map.with_index 110.948k (± 3.4%) i/s - 560.508k in 5.058655s each_with_index 115.360k (± 2.2%) i/s - 583.923k in 5.064205s Comparison: lazy.zip.map: 186024.9 i/s each_with_index: 115360.3 i/s - 1.61x slower map.with_index: 110947.8 i/s - 1.68x slower zip.map: 81510.6 i/s - 2.28x slower
- Loading branch information