Skip to content

Commit

Permalink
Use Enumerator::Lazy#zip for better performance
Browse files Browse the repository at this point in the history
  $ 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
aroben committed Feb 17, 2017
1 parent 6dafc25 commit 8846810
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/simplecov/raw_coverage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def merge_resultsets(result1, result2)
def merge_file_coverage(file1, file2)
return (file1 || file2).dup unless file1 && file2

file1.zip(file2).map do |count1, count2|
file1.lazy.zip(file2).map do |count1, count2|
merge_line_coverage(count1, count2)
end
end
Expand Down

0 comments on commit 8846810

Please sign in to comment.