From: Ruby Maniac Date: 2007-09-29T04:30:06+09:00 Subject: Re: Ruby performance On Sep 28, 8:59 am, Vasyl Smirnov wrote: > Hi, > > I'm new to Ruby, and I've just tried to measure it's performance > compared to Python, and got some interesting results. > > The Ruby test script is: > > #!/usr/bin/env ruby > > s = "" > i = 0 > while line = gets > s += line > i += 1 > puts(i) if i % 1000 == 0 > end > > and the Python one: > > #!/usr/bin/env python > > import sys > > s = "" > i = 0 > for line in sys.stdin: > s += line > i += 1 > if i % 1000 == 0: print i > > I fed a 1MB-large file as input to each of this script. The strange > thing is that the Ruby version starts to output progress slower and > slower as the s string grows. The Python version went smoothly. > > Looks like a memory management issue with Ruby. I wonder if this is > going to be significantly improved in subsequent releases of Ruby? I > was testing with Ruby 1.8.6, and Python 2.5.1. (I also tried Python > 2.3 and it was significantly slower than 2.5) > > As I said, I'm new to Ruby, but want to use it for a long term > project, and would like to know more about it's performance specifics. > > Thanks for any clarification in advance! Some things about optimizing Python that doesn't work in Ruby except to make the Ruby code run more slowly: 1). Read the entire contents of the file using a single operation. 2). Use LC (List Comprenhensions) to iterate over the entire file such as to count or gather lines. 3). Use Psyco to dramatically boost the speed of the Python code to near machine code speeds. 4). Use String translate function to process the contents of the file in cases where this sort of thing is needed. I was able to read a 20 MB file using Python code that ran 10x to 30x faster than the fastest Ruby code that attempted to read the file into an array of 64 bit values. In my case my code needed to process every single character in the 20 MB file by setting the MSB of each character and then write the file back out.