From: "Glenn M. Lewis" Date: 2005-09-09T12:46:30+09:00 Subject: Optimizing a single slow method Hi! I have a Ruby script (a small part of it is included below) that has many methods, but one method is so incredibly slow that I would like to write it in C++ (or any other faster language). The method is "Contract.parseFile(file)". I've tried the latest SWIG, but am having a heck of a time trying to call the two methods: "Contract.open()" and "Contract.addTick()" from within the SWIG'd extension. (By the way, I also tried "rio(file).csv {|fields|" but that had the same speed as the code below.) Any ideas on how I can rewrite 'Contract.parseFile()' for speed? Thanks! -- Glenn Lewis class Contract @@contracts = Hash.new @@files = Hash.new DataDir = "c:/src/Data" MonthNames = %w(nil jan feb mar apr may jun jul aug sep oct nov dec) def Contract.open(contract) @@contracts.has_key?(contract) ? @@contracts[contract] : Contract.new(contract) end def Contract.all @@contract.keys end def Contract.parseFile(file) return unless File.exists?(file) return if @@files.has_key?(file) @@files[file] = 1 print "Parsing file #{file}..." File.open(file, "rb").each_line {|line| line.chomp! # puts line fields = line.split(/,/) next if fields.size < 8 datestring = fields[1] year = datestring[0..1].to_i month = datestring[2..3].to_i day = datestring[4..5].to_i # puts "year=#{year}, month=#{month}, day=#{day}" # RUBY BUG?!? Can't use 'date' here... as that messes up findTick's 'date'... mydate = Date.new((year < 50 ? year+2000 : year+1900), month, day) contract = Contract.open(fields[0]) tick = Tick.new(mydate, fields[2].to_f, fields[3].to_f, fields[4].to_f, fields[5].to_f, fields[6].to_i, fields[7].to_i) contract.addTick(tick) } puts "done" end def initialize(contract) @contract = contract @@contracts[contract] = self @ticks = Hash.new end def addTick(tick) @ticks[tick.date.to_s[0..9]] = tick end end