From: Rick DeNatale Date: 2007-10-25T02:58:45+09:00 Subject: Re: Struct is slow On 10/24/07, Wayne Magor wrote: > Just wanted to follow-up so people don't think there might be problems > with the Struct class. It isn't actually slow at all. The problem was > just that an inner loop was not converted from an array and so didn't > work and used a lot of CPU resources. Once fixed, I can't tell any > difference between the two versions. > > It was this: > > type_arr.each do |td_type, td_name| > if t == td_name then > primary_t = td_type > break > end > end > > needed to be converted to this: > > type_arr.each do |x| > if t == x.typedef_name then > primary_t = x.typedef_type > break > end > end > > I used a different naming scheme in that loop (td_name instead of > typedef_name) which caused me not to find it when doing a search for all > places that needed to be converted to a struct. > > This is one example where strong typing could have made a difference. > Had the array been declared as an array of arrays or array of structs, > perhaps another language could have detected the error. I'm not > advocating for strongly typed languages, I'm just saying that some kinds > of errors could be harder to find in Ruby. What do you folks think? What would have made a bigger difference would be to develop that code in the contest of specifications written as tests. This would allow you to first concentrate on making the code do the right thing before trying to optimize it. I usually find that it really doesn't matter how fast things run if they produce the wrong results. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/