Glenn M. Lewis <noSpam / noSpam.com> wrote:
> 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.)

First of all I'd try to exactly determine *why* it is slow and where it's 
spending it's time.  Since you're doing file IO chances are that a version 
in a different language is not much faster.

Also, as a hint for a pure Ruby solution: did you try the library functions 
for date parsing?  That might make up a significant part of the time spent 
here.  Also, an alternative is to use a regular expression to match the 
entire line instead of splitting it.  That way you can verify that the 
current line matches your expectaitions (aka regexp) and have direct access 
to field values and can verify that.

Also, I noticed that you don' close your file.  I always prefer the block 
form

File.open(file, "rb") do |io|
  io.each_line do |line|
    line.chomp!
    ...
  end
end

Kind regards

    robert