Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 在大数组上进行加法的有效方法_Ruby - Fatal编程技术网

Ruby 在大数组上进行加法的有效方法

Ruby 在大数组上进行加法的有效方法,ruby,Ruby,我有一个包含+20000个整数元素的数组 我想创建一个新数组,其中旧数组中的每个元素都添加了一个修改数字。在一个小样本阵列上,它将如下所示: old_array = [2,5,6,8] modifying_number = 3 new_array = [5,8,9,11] 有没有比这样的迭代更有效的方法 class Array def addition_by(x) collect { |n| n + x } end end 否。N迭代是该算法的最小复杂性 您可以通过使用col

我有一个包含+20000个整数元素的数组

我想创建一个新数组,其中旧数组中的每个元素都添加了一个修改数字。在一个小样本阵列上,它将如下所示:

old_array = [2,5,6,8]
modifying_number = 3
new_array = [5,8,9,11]
有没有比这样的迭代更有效的方法

class Array
  def addition_by(x)
    collect { |n| n + x }
  end
end

否。
N
迭代是该算法的最小复杂性


您可以通过使用
collect修改源数组来完成此操作(如果出于某些原因不需要源数组)。复杂性将是相同的,不会创建额外的大对象。

20k记录不需要担心性能

ary = Array.new(20000) { 1 } 
ary.map! { |el| el + 1 }
会很好用的


我只是建议修改初始数组,而不是创建一个新的数组(使用带有bang的方法),这样它肯定会使用更少的资源。

我想这意味着用另一种方式实现
map
?处理这样的任务。我已经包括了@JörgWMittag和@uishra的答案基准。尽管必须指出,
速度
不是相关问题的要求,因此答案在这方面不能受到批评。我还包括了@CarySwoveland对这个问题的回答

require 'fruity'
require 'matrix'

class Array
  #jörg_w_mittag
  def new_map
    return enum_for(__callee__) unless block_given?
    inject([]) {|acc, el| acc << yield(el) }
  end

  #uishra
  def my_map(&block)
    result = []
    each do |element|
     result << block.call(element)
    end
    result
  end

  #cary_swoveland
  def vec_map(k)
    (Vector[*[k]*self.size] + Vector[*self]).to_a
  end

end

arr = (1..30000).to_a
k = 3

10.times do
  compare do
    core_map       { ar = arr.dup; ar.map     { |n| n + k } }
    jörg_w_mittag  { ar = arr.dup; ar.new_map { |n| n + k } }
    uishra         { ar = arr.dup; ar.my_map  { |n| n + k } }
    cary_swoveland { ar = arr.dup; ar.vec_map k }
  end
  puts
end
两次结果

#Running each test once. Test will take about 1 second.
#core_map is faster than jörg_w_mittag by 2x ± 1.0
#jörg_w_mittag is similar to uishra
#uishra is similar to cary_swoveland
#Running each test once. Test will take about 1 second.
#core_map is faster than jörg_w_mittag by 2x ± 0.1
#jörg_w_mittag is similar to uishra
#uishra is similar to cary_swoveland
#Running each test once. Test will take about 1 second.
#core_map is faster than uishra by 2x ± 1.0
#uishra is similar to jörg_w_mittag
#jörg_w_mittag is similar to cary_swoveland
三次结果

#Running each test once. Test will take about 1 second.
#core_map is faster than jörg_w_mittag by 2x ± 1.0
#jörg_w_mittag is similar to uishra
#uishra is similar to cary_swoveland
#Running each test once. Test will take about 1 second.
#core_map is faster than jörg_w_mittag by 2x ± 0.1
#jörg_w_mittag is similar to uishra
#uishra is similar to cary_swoveland
#Running each test once. Test will take about 1 second.
#core_map is faster than uishra by 2x ± 1.0
#uishra is similar to jörg_w_mittag
#jörg_w_mittag is similar to cary_swoveland

萨加尔,你能把我的方法添加到你的基准中吗?我这样做了,结果打成平局。顺便说一句,我运行了好几次基准测试。有时乌斯拉比约尔古·维特格·米塔格跑得快,有时则不然。等一下。Jörg的答案在哪里?@CarySwoveland他在:)@CarySwoveland中对答案进行了基准测试,我已经更新了基准测试。我最初的答案(和这个答案一样)包括了Jörg的答案,但我只是稍微更改了他的方法名称,但实现是一样的。如果你觉得我需要改进我的答案,请告诉我。顺便说一句,我第一次发现
furty.rb
是从你之前的一个SO答案中找到的,所以ta对此表示感谢!你需要感谢@theTinMan。他被提升的
furty
职位比我能统计的还要多。看来核心
map
赢得了这一天。现在的问题似乎是Ruby的哪个实现具有最快的
map