From: Josh Cheek Date: 2012-02-04T02:43:09+09:00 Subject: Re: Basic Ruby performance --f46d04388ee96f30a704b812d807 Content-Type: text/plain; charset=ISO-8859-1 On Fri, Feb 3, 2012 at 11:13 AM, Dmitry Nikiforov wrote: > Robert Klemme wrote in post #1043884: > > > Question: the data needs to come from somewhere. Are you sure that > > your processing is CPU bound? If it is IO bound the difference > > between Perl and Ruby won't really show. I reckon it's better to > > create a more realistic example of what you are trying to do and > > measure again. (And take care to run tests between Ruby and Perl > > alternating in order to prevent OS IO caching from preferring one or > > the other.) > > Yep, I'm sure it's CPU bound: the CPU load is at 100%. The data comes in > faster than the Ruby script can process it, unfortunately, at this > point. I'm trying to optimize it, of course, but so far Perl version > beats Ruby hands down. But there aren't too many options at my disposal, > it seems. In my examples, the "for" seems to be the major culprit: it > alone, without ANYTHING within the loop, takes 19 seconds to execute 1E8 > times. The (0..1E8).each only saves about 1 second for me. Which doesn't > really matter - most of the loops in my scripts are "while" loops > anyway. Still, the regexps themselves run very slow. I wish Ruby used > standard Perl's PCRE library - that would make at least regexps run as > fast as they do in Perl, and I would be able to write my scripts in Ruby > :) > > -- > Posted via http://www.ruby-forum.com/. > > It creates a Range, which just iterates, not an array. A more idiomatic way would probably be (1+10**8).times { ... } As an aside, if all the processing is happening in the loop, then it might make more sense that the loop just delegates work out to other processes (e.g. parse a line or process a parsed set of data). This could be pretty simple if done with a thread pool in a single Ruby script (you'll want one of the alternate implementations here since you're CPU bound and MRI has a GIL), or as arbitrarily complex as you like. --f46d04388ee96f30a704b812d807--