Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
Rails batch convert one object to another object

I am looking for the most efficient way (in speed) to converts a huge number of objects (1M instances) to another object type. Unfortunately I don't have the choice of what I am getting as an input (the million object).

So far I've tried with each_slice but it does not show much improvement when it comes to speed!

It looks like this:

expected_objects_of_type_2 = []
huge_array.each_slice(3000) do |batch|
  batch.each do |object_type_1|
    expected_objects_of_type_2 << NewType2.new(object_type_1)
  end  
end

Any idea?

Thanks!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I did a quick test with a few different methods of looping the array and measured the timings:

huge_array = Array.new(10000000){rand(1..1000)}
a = Time.now
string_array = huge_array.map{|x| x.to_s}
b = Time.now
puts b-a

Same with:

sa = []
huge_array.each do |x|
    sa << x.to_s
end

and

sa = []
huge_array.each_slice(3000) do |batch|
  batch.each do |x|
    sa << x.to_s
  end  
end 

No idea what you are converting so I did a bit of simple int to string.

Timings

Map: 1.7
Each: 2.3
Slice: 3.2

So apparently your slice overhead makes things slower. Map seems to be the fastest (which is internally just a for loop but with a non-dynamic length array as output). The << seems to slow things down a bit.

So if each object needs an individual converting you are stuck with O(n) complexity and can't speed things up by a lot. Just avaid overhead.

Depending on your data, sorting and exploiting caching effects might help or avoiding duplicates if you have a lot of identical data but we have no way to know if we don't know your actual conversions.

over 4 years ago · Santiago Trujillo Relatório

0

I would treat each slice in its own thread:

huge_array.each_slice(3000) do |batch|
  Thread.new do 
    batch.each do |object_type_1|
      expected_objects_of_type_2 << NewType2.new(object_type_1)
    end  
  end
end

Then you have to wait for the threads to terminate using join. They should be accumulated in an array and joined.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda