From: Daniel Berger Date: 2006-04-13T02:06:11+09:00 Subject: Re: First script seems slow - What's a better way to write t ara.t.howard@noaa.gov wrote: > On Thu, 13 Apr 2006, Charlotte wrote: > >> Thanks for the link. However, I forgot to mention that the reason I'm >> doing this myself is because no currently available tools like that work >> with my code, as it contains commands specific to the program it extends >> and gives errors telling me that they are invalid command names. >> >> Basically, I'm just trying to figure out if it's my fault the script is >> slow, or if Ruby just isn't very efficient. >> >>> I realise that this does nothing for your Ruby but perhaps it will help >>> you get onto something more interesting :) >> >> This is more interesting ;) >> >> On a side note, so far I like Ruby better than tcl. > > how does this do (untested) : > > harp:~ > cat a.rb > > require 'yaml' > class TclIndex > def initialize arg > @procs, @vars = {}, {} > parse arg > end > def parse a > read = lambda{|io| io.readlines.map!{|l| l.gsub %r/#.*$/, ''}} > lines = a.respond_to?('readlines') ? read[a] : open(a){|f|read[f]} > lines.each do |line| > case line > when %r/^ \s* proc \s+ (\w+)/iox > @procs[$1] = -1 > when %r/^ \s* set \s+ (\w+)/iox > @vars[$1] = 0 > end > @procs.keys.each{|k| @procs[k] += 1 if line[%r/\b#{ k }\b/]} > @vars.keys.each{|k| @vars[k] += 1 if line[%r/\b#{ k }\b|\$#{ k > }\b/]} > end > end > def report o > o << { > 'procs' => @procs.to_a.sort_by{|ab| ab.last}.map{|ab| Hash[*ab]}, > 'vars' => @vars.to_a.sort_by{|ab| ab.last}.map{|ab| Hash[*ab]}, > }.to_yaml > end > end > > > abort "Usage: [inputfilepath = stdin] [outputfilename = stdout]" if > ARGV.delete('help') or ARGV.delete('--help') > > i = ARGV.shift || STDIN > o = ARGV.shift || STDOUT > > idx = TclIndex.new i > idx.report o > > regards. > > -a Just a quick note - precompiling the regular expressions might help here, too. Regards, Dan