From: gerry@... (Gerry Wiener) Date: 2001-08-16T02:32:41+09:00 Subject: [ruby-talk:19809] how to speed up hash script? I'm in the process of writing a script that will read data from a 17MB climatology file and then hash the information according to date-time. The script below takes approximately 14 seconds to run. An analogous Python script takes 3 seconds to run. Is there a simple way to speed up the Ruby script? I'm interested in the possibility of using Ruby as an alternative to Python. Thanks, Gerry Wiener class Climo def initialize(in_file) # Read in data @fp = File.new(in_file, "r") @lines = @fp.readlines() @fp.close() @num_lines = @lines.size() @date_format = "YYMMDDHH" @len_date = @date_format.size() # Store line indices in dictionary based on date field @dict = Hash.new() for i in 1...@num_lines @dict[@lines[i][0...@len_date]] = i end first_date = @lines[1][0...@len_date] last_date = @lines[@num_lines-1][0...@len_date] puts "first_date: ", first_date puts "last_date: ", last_date end end def main() in_file = ARGV[0] climo_obj = Climo.new(in_file) end main() --------------------------------------------------------------------------------- Here's a sample of the underlying data: WZW 60.1 132.45 80010210 -9 -9 -99.00 -9 -9 -9 -99.0 -8 -16 -99 290 6 0 -99.000 -99.000 -99.000 80010213 -9 -9 -99.00 -9 -9 -9 -99.0 -8 -16 -99 290 7 0 -99.000 -99.000 -99.000 80010214 -9 -9 -99.00 -9 -9 -9 -99.0 -8 -16 -99 280 4 0 -99.000 -99.000 -99.000 80010215 -9 -9 -99.00 -9 -9 -9 -99.0 -8 -16 -99 270 7 0 -99.000 -99.000 -99.000 80010216 -9 -9 -99.00 -9 -9 -9 -99.0 -8 -16 -99 290 5 0 -99.000 -99.000 -99.000 80010401 -9 -9 -99.00 -9 -9 -9 -99.0 -7 -14 -99 300 9 0 -99.000 -99.000 -99.000 80010402 -9 -9 -99.00 -9 -9 -9 -99.0 -7 -14 -99 290 8 18 -99.000 -99.000 -99.000 80010403 -9 -9 -99.00 -9 -9 -9 -99.0 -7 -16 -99 330 8 0 -99.000 -99.000 -99.000 80010404 -9 -9 -99.00 -9 -9 -9 -99.0 -7 -16 -99 300 12 20 -99.000 -99.000 -99.000 80010405 -9 -9 -99.00 -9 -9 -9 -99.0 -8 -17 -99 290 10 18 -99.000 -99.000 -99.000 80010406 -9 -9 -99.00 -9 -9 -9 -99.0 -8 -17 -99 300 10 0 -99.000 -99.000 -99.000 80010407 -9 -9 -99.00 -9 -9 -9 -99.0 -10 -19 -99 300 11 16 -99.000 -99.000 -99.000