What's the efficient way to multiply two arrays and get sum of multiplied values in Ruby?

前端 未结 9 894
猫巷女王i
猫巷女王i 2021-02-09 12:54

What\'s the efficient way to multiply two arrays and get sum of multiply values in Ruby? I have two arrays in Ruby:

array_A = [1, 2, 1, 4, 5, 3, 2, 6, 5, 8, 9]
a         


        
9条回答
  •  误落风尘
    2021-02-09 13:04

    EDIT: Vector is not fastest (Marc Bollinger is totally right).

    Here is the modified code with vector and n-times:

    require 'benchmark'
    require 'matrix'
    
    array_A = [1, 2, 1, 4, 5, 3, 2, 6, 5, 8, 9]
    array_B = [3, 2, 4, 2, 5, 1, 3, 3, 7, 5, 4]
    
    vector_A = Vector[*array_A]
    vector_B = Vector[*array_B]
    
    def matrix_method a1, a2
      (Matrix.row_vector(a1) * Matrix.column_vector(a2)).element(0,0)
    end
    
    def vector_method a1, a2
      a1.inner_product(a2)
    end
    
    n = 100000
    
    Benchmark.bmbm do |b|
      b.report('matrix method') { n.times { matrix_method(array_A, array_B) } }
      b.report('array walking') { n.times { (0...array_A.count).to_a.inject(0) {|r, i| r + array_A[i]*array_B[i]} } }
      b.report('array walking without to_a') { n.times { (0...array_A.count).inject(0) {|r, i| r + array_A[i]*array_B[i]} } }
      b.report('array zip') { n.times { array_A.zip(array_B).map{|i,j| i*j }.inject(:+) } }
      b.report('array zip 2') { n.times { array_A.zip(array_B).inject(0) {|r, (a, b)| r + (a * b)} } }
      b.report('while loop') do
        n.times do
          sum, i, size = 0, 0, array_A.size
          while i < size
            sum += array_A[i] * array_B[i]
            i += 1
          end
          sum
        end
      end
      b.report('vector') { n.times { vector_method(vector_A, vector_B) } }
    end
    

    And the results:

    Rehearsal --------------------------------------------------------------
    matrix method                0.860000   0.010000   0.870000 (  0.911755)
    array walking                0.290000   0.000000   0.290000 (  0.294779)
    array walking without to_a   0.190000   0.000000   0.190000 (  0.215780)
    array zip                    0.420000   0.010000   0.430000 (  0.441830)
    array zip 2                  0.340000   0.000000   0.340000 (  0.352058)
    while loop                   0.080000   0.000000   0.080000 (  0.085314)
    vector                       0.310000   0.000000   0.310000 (  0.325498)
    ----------------------------------------------------- total: 2.510000sec
    
                                     user     system      total        real
    matrix method                0.870000   0.020000   0.890000 (  0.952630)
    array walking                0.290000   0.000000   0.290000 (  0.340443)
    array walking without to_a   0.220000   0.000000   0.220000 (  0.240651)
    array zip                    0.400000   0.010000   0.410000 (  0.441829)
    array zip 2                  0.330000   0.000000   0.330000 (  0.359365)
    while loop                   0.080000   0.000000   0.080000 (  0.090099)
    vector                       0.300000   0.010000   0.310000 (  0.325903)
    ------
    

    Too bad. :(

提交回复
热议问题