简体   繁体   中英

Ruby Array Operations: Increasing Efficiency

In preparation for manipulation of a large chunk of data, I perform some cleaning and pruning operations prior to processing it. The code I have functions fine, but I'm worried about how these operations will scale up when I perform this on millions of points. The code I have feels inefficient but I don't know how to simplify my steps.
In my process, I parse in a CSV file, check for garbage data ie non-numerical values, typecast the remaining data to floats, and then sort it. I'm hoping for some guidance on how to improve this if possible.

require 'green_shoes'
require 'csv'

class String
    def valid_float?
        true if Float self rescue false
    end
end

puts "Parsing..."

temp_file = ask_open_file("")
temp_arr = CSV.read(temp_file)

temp_arr.each do |temp_row|
    temp_row.map!{ |x| !x.valid_float? ? 0 : x }
    temp_row.map!{ |x| x.to_f}
    temp_row.sort!
end

My guess is that you'd want to return the file contents when you are done, right? If so, you'll want to use map on temp_arr , instead of each .

You can save an iteration by combining first two lines together:

temp_arr.map! do |temp_row|
  temp_row.map!{ |x| x.valid_float? ? x.to_f : 0.0 }
  temp_row.sort!
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM