From: Dennis Decker Jensen Date: 2001-05-06T04:16:27+09:00 Subject: [ruby-talk:14719] SV: Why's Ruby so slow in this case? Stefan Matthias Aust wrote: | I wonder whether there are some ways to optimize this code other than | to completely change the algorithm. Or can the Ruby interpreter be | improved? I'm using Ruby 1.6.4 if that matters. Oh! I just remembered. Predefining variables used in a loop should increase the performance AFAIK. | def initmap | count = $mapsize * $mapsize | $hexes = (0...count).map{Hex.new} | | $hexes[rand(count)].terrain = Terrain::Mountain | | for i in 0...(count / 2 - 1) | a = [] h = 0 ^^^^^ | for h in 0...count | if offshore(h) then a << h end | end | h = a[rand(a.length)] | $hexes[h].terrain = | case rand(3) | when 0; Terrain::Plain | when 1; Terrain::Forest | when 2; Terrain::Mountain | end | end | end | | def offshore(h) #private | if $hexes[h].iswater d = h2 = 0 ^^^^^^^^^^ | for d in 0..5 | h2 = displace(h, d) | if h2 >= 0 && !$hexes[h2].iswater | return true | end | end | end | false | end Hope this helps a bit. Best regards, Dennis Decker Jensen