I have a script written in Ruby that has maxed out a core in my server's Xeon processor for the last 2 hours. Since it's currently only using 1 of four possible cores, I want to try and rewrite the script to take advantage of all four cores.
I can use the .each_slice(n) method on the array that contains my data, but I'm curious as to what would then be the best/most efficient way to then write this data to a file. It seems that I have a couple options.
Pass the file object to the functions being called by the Thread.new function (I assume this is legal in ruby?) and have them write as they see fit.
Have each function store the results in arrays, return the array on completion and let the main program then write to the disk.
Pass the same array object to each item and have them all add to it, then sort it.
My assumption is that although 2 would probably be more memory intensive, it'll by far be most efficient method. Is there a different way to accomplish what I'm doing?