如何在Ruby中使用两个数组?

时间:2022-09-06 15:35:43

I have two arrays, for example

我有两个数组

a = [3, 2, 1]
b = [1, 2, 3]

I need to multiply them and create a third array c that will be like this

我需要将它们相乘然后创建第三个数组c,就像这样。

c = [3 * 1, 2 * 2, 1 * 3]

Which method is the best by speed? I need to do this for huge arrays, and time is important.

哪种方法在速度上是最好的?我需要为巨大的数组做这个,时间很重要。

4 个解决方案

#1


9  

a.zip(b).map{|x, y| x * y}

This works because zip combines the two arrays into a single array of two element arrays. i.e.:

这之所以有效,是因为zip将两个数组组合为两个元素数组的单个数组。例如:

a = [3, 2, 1]
b = [1, 2, 3]
a.zip(b)
#=> [[3, 1], [2, 2], [1, 3]]

Then you use map to multiply the elements together. This is done by iterating through each two element array and multiplying one element by the other, with map returning the resulting array.

然后使用map将元素相乘。这是通过遍历每个两个元素数组并将一个元素乘以另一个来实现的,映射返回结果数组。

a.zip(b).map{|x, y| x * y}
#=> [3, 4, 3]

#2


2  

Try this:

试试这个:

[[3,2,1],[1,2,3]].transpose.map {|a| a.inject(:*)}

#3


1  

You can try this:

你可以试试这个:

a.map.with_index{ |x, i| a[i]*b[i]}

#4


0  

Since you want to multiply two arrays, we have to assume they are of same size.

由于要将两个数组相乘,我们必须假设它们的大小相同。

Hence, below is a simple way to multiply them - it has O(n) time complexity. Other answers are also equally good, you can pick any one

因此,下面是一种将它们相乘的简单方法——它具有O(n)时间复杂度。其他答案也一样好,你可以随便选一个

a = [3, 2, 1]
b = [1, 2, 3]

a.size.times.collect { |i| a[i] * b[i] }
#=> [3, 4, 3]

If time is really of the essence, then, you may want to use multiple threads. A sample program demonstrating the concept is shown below. You can build upon it based on your specific needs.

如果时间真的很重要,那么您可能需要使用多个线程。演示该概念的示例程序如下所示。您可以基于您的特定需求构建它。

a = [3, 2, 1]
b = [1, 2, 3]

num_cores = 2 # This decides number of threads, ideally equal to number of cores
ary_size = a.size

# We need to collect result of each thread as separate sub-array.
# We will use result array-of-array so that order of result is preserved
results = Array.new(num_cores, [])

# Divide the array indexes into equal parts    
index_splits = (0...ary_size).each_slice(num_cores)

threads = []

# Process each sub-array in a thread of its own
index_splits.each_with_index do |ary, i| 
    threads << Thread.new do
      results[i] = ary.collect {|j| a[j] * b[j] } 
    end
end
threads.each {|t| t.join} 

p results.flatten
#=> [3,4,3]

#1


9  

a.zip(b).map{|x, y| x * y}

This works because zip combines the two arrays into a single array of two element arrays. i.e.:

这之所以有效,是因为zip将两个数组组合为两个元素数组的单个数组。例如:

a = [3, 2, 1]
b = [1, 2, 3]
a.zip(b)
#=> [[3, 1], [2, 2], [1, 3]]

Then you use map to multiply the elements together. This is done by iterating through each two element array and multiplying one element by the other, with map returning the resulting array.

然后使用map将元素相乘。这是通过遍历每个两个元素数组并将一个元素乘以另一个来实现的,映射返回结果数组。

a.zip(b).map{|x, y| x * y}
#=> [3, 4, 3]

#2


2  

Try this:

试试这个:

[[3,2,1],[1,2,3]].transpose.map {|a| a.inject(:*)}

#3


1  

You can try this:

你可以试试这个:

a.map.with_index{ |x, i| a[i]*b[i]}

#4


0  

Since you want to multiply two arrays, we have to assume they are of same size.

由于要将两个数组相乘,我们必须假设它们的大小相同。

Hence, below is a simple way to multiply them - it has O(n) time complexity. Other answers are also equally good, you can pick any one

因此,下面是一种将它们相乘的简单方法——它具有O(n)时间复杂度。其他答案也一样好,你可以随便选一个

a = [3, 2, 1]
b = [1, 2, 3]

a.size.times.collect { |i| a[i] * b[i] }
#=> [3, 4, 3]

If time is really of the essence, then, you may want to use multiple threads. A sample program demonstrating the concept is shown below. You can build upon it based on your specific needs.

如果时间真的很重要,那么您可能需要使用多个线程。演示该概念的示例程序如下所示。您可以基于您的特定需求构建它。

a = [3, 2, 1]
b = [1, 2, 3]

num_cores = 2 # This decides number of threads, ideally equal to number of cores
ary_size = a.size

# We need to collect result of each thread as separate sub-array.
# We will use result array-of-array so that order of result is preserved
results = Array.new(num_cores, [])

# Divide the array indexes into equal parts    
index_splits = (0...ary_size).each_slice(num_cores)

threads = []

# Process each sub-array in a thread of its own
index_splits.each_with_index do |ary, i| 
    threads << Thread.new do
      results[i] = ary.collect {|j| a[j] * b[j] } 
    end
end
threads.each {|t| t.join} 

p results.flatten
#=> [3,4,3]