So I have an array of 40000 elements, and I wish to run a method on each element.
To reduce the time it will take, I am thinking of running this on multiple threads. Perhaps splitting the array into multiple arrays and running on a different thread or something. But I don't know how to get started.
Say the array is foo[], and the method to call is bar(). bar() returns a string. After the code executes, I want to be able to combine/add up all the strings together in one big string.
Is there a way to do this? I tried to keep my question as simple as possible, but if you want more information, let me know.
Thanks in advance!
It sounds like you want something like a pmap function. There is a ruby library called peach that provides both a pmap and a peach ("parallel" each) method on the native arrays.
With this library, you can do something like
require 'peach'
[1,2,3,4].pmap{|x| f(x)} #Spawns 4 threads, => [f(1),f(2),f(3),f(4)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With