From: "Christoffer Lernö" Date: 2007-09-29T02:27:34+09:00 Subject: Re: Ruby performance On 28 Sep 2007, at 19:21, John Joyce wrote: > > On Sep 28, 2007, at 11:06 AM, khaines@enigo.com wrote: > >> On Sat, 29 Sep 2007, 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. >> >> This is because "string" + "otherstring" allocates a new string, >> "stringotherstring". >> >> Use << instead, and it will concatenate "otherstring" to the >> orignal "string". >> >> s = "" >> i = 0 >> while line = gets >> s << line >> i += 1 >> puts(i) if i % 1000 == 0 >> end >> >> Performance should be a lot better with that version. >> >> >> Kirk Haines >> >> > That should be on a Ruby Optimization list somewhere! Weird. The python version extremely slow compared to Kirk's ruby version. Does it suffer from the same allocation problems as the initial ruby version? /C