From: Charlotte Date: 2006-04-13T04:24:02+09:00 Subject: Re: First script seems slow - What's a better way to write t Brian Mitchell wrote: > I get something like this on 1.8 cvs: > > user system total real > foo 4.920000 0.080000 5.000000 ( 5.581873) > bar 4.610000 0.060000 4.670000 ( 5.457461) > qux 15.280000 0.280000 15.560000 ( 17.514639) Just for fun, I tried it too... I get > user system total real >foo 5.141000 0.000000 5.141000 ( 5.157000) >bar 4.765000 0.032000 4.797000 ( 4.812000) >qux 22.219000 1.593000 23.812000 ( 23.906000) Hmm... I know for a fact that my (work) computer is messed up, but still - it's 3.0Ghz HT P4 with 1Gb RAM. Running Windows XP, as much disabled as I can to try and convince the thing to run quickly. Also, I tried the YAML version - at first it told me that I couldn't modify a frozen string. I read somewhere about this happening if you try to modify an ARGV value, so I changed > i = ARGV.shift || STDIN > o = ARGV.shift || STDOUT to > i = File.open(ARGV[0], 'r') > o = File.new(ARGV[1], 'w') I don't know if that has an effect on the speed or not, but it worked. Speed results: My script: 450 seconds YAML script: 156 seconds My bottleneck definitely seems to be iterating through each line in the input file: % cumulative self self total time seconds seconds calls ms/call ms/call name 70.25 316.08 316.08 327 966.61 1369.08 IO#each 15.07 383.88 67.79 991826 0.07 0.07 String#[] 14.15 447.56 63.69 984426 0.06 0.06 String#+ 0.11 448.05 0.49 2 243.00 406.50 Hash#sort ... etc. Hmm... seems like it would be worthwhile to learn all that lambda/map/#&@^#* gibberish. Thanks! -- Posted via http://www.ruby-forum.com/.