From: nobu.nokada@... Date: 2002-03-19T11:24:03+09:00 Subject: Re: Why is Ruby so slow? Hi, At Tue, 19 Mar 2002 03:41:31 +0900, Venherm Borchers wrote: > WHY IS RUBY SO SLOW? > > I implemented a _DataReader_ class in Ruby and Python. The reader: (snip) > Here are the running times for some available Ruby implementations > under Windows: > > ________data items______1,600,000_________320,000_______ > > Ruby 1.6.5-2 17:10 min 46 sec > Ruby 1.6.6-0 18:43 min 58 sec > Ruby 1.7.2 (i586-mswin32) 18:05 min 54 sec tData.load ran in 2.824 secs, but tData.prelyze spent 48.655 secs, it's exactly too slow. > def prelyze(logging=false, missing=@missing) > t1 = Time.now > dtypes = {0 => 'NA', 1 => 'Integer', 2 => 'Continuous', > 3 => 'String', 4 => 'Set'} > @dtypes = [] > for j in (0...@ncols) do > ctype = 0; mitms = 0 > @col[j].each { |item| > if item == missing > ctype = [ctype, 0].max > mitms += 1 > elsif item =~ /^\s*[+\-]?\d+\s*$/ > ctype = [ctype, 1].max > elsif item =~ /^\s*[+\-]?(?:\d+\.\d*|\d*\.\d+)\s*$/ > ctype = [ctype, 2].max > else > ctype = [ctype, 3].max > end > } > > nitms = (@col[j]-['']).nitems Possibly here. Array#- makes a hash once so a little expensive. Try with: nitms = @col[j].nitems - @col.grep(/^$/).nitems Or it may be better to count nitms up in @col[j].each block. -- Nobu Nakada