------art_20564_27343375.1173623500925
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On 3/11/07, Robert Dober <robert.dober / gmail.com> wrote:
> Code's slow :(
> post's fast ;)
>
> hopefully I find some time to profile and optimise the whole thing.
>
Interesting case of the profiler showing bad design, the Neighborhood
class was really bad this one makes the code run twice as fast, still
slow but less so

590/91 > cat torus.rb
---------------------------
<snip>
class Neighborhood
  include Enumerable

  # Neighborhood gives us the following indexed view to the underlying
  # torus
  #     +---+---+     +-----------+-----------+
  #     | 0 | 1 |     | @top,@lft | @top,@rgt |
  #     +---+---+     +-----------+-----------+
  #     | 3 | 2 |     | @bot,@lft | @bot,@rgt |
  #     +---+---+     +-----------+-----------+
  #

  def initialize *args
    @torus, @top, @bottom, @left, @right  args
    @names   [@top, @left], [@top, @right], [@bottom, @right],
[@bottom, @left] ]
  end

  def [] n
    @torus[ *@names[n%4]  ]
  end
  def [] , val
    @torus[ *@names[n%4] ]  al
  end

  def each
    4.times do
      | idx |
      yield self[idx]
    end
  end

  def recalc
    if any?{|v| v ICE} then
      4.times do
        | idx |
        self[ idx ]  CE if self[ idx ] VAPOR
      end
    else
      rotate( rand(2) )
    end
  end

  def rotate dir
    x  elf[0]
    3.times do
      | n |
      self[ n + 2*dir*n ]  elf[ n + 1 + dir*2*n.succ ]
    end # 3.times do
    self[ 3 + 2 * dir ]  
  end # def rotate dir

end # class Neighborhood

------art_20564_27343375.1173623500925
Content-Type: application/x-ruby; name=torus.rb
Content-Transfer-Encoding: base64
X-Attachment-Id: f_ez5joa46
Content-Disposition: attachment; filename="torus.rb"

IyB2aW06IHN3PTIgc3RzPTIgbnUgdHc9MCBleHBhbmR0YWIgbm93cmFwOg0KIw0KIw0KDQpJQ0Ug
PSBDbGFzcy5uZXcNClZBUE9SID0gQ2xhc3MubmV3DQpWQUNVVU0gPSBDbGFzcy5uZXcNCg0KDQoj
IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjI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kIEBuaG9vZHNbIGxpbmVhcl9pZHgoIHIsIGMgKSBdIHx8PSANCiAgICAg
ICAgICBOZWlnaGJvcmhvb2QubmV3KCBzZWxmLCByLCByLnN1Y2MgJSBAbGluZXMsIGMsIGMuc3Vj
YyAlIEBjb2x1bW5zICkNCiAgICAgIGMgKz0gMg0KICAgICAgciArPSAyIHVubGVzcyBjIDwgQGNv
bHVtbnMNCiAgICAgIHJldHVybiB1bmxlc3MgciA8IEBsaW5lcw0KICAgICAgYyAlPSBAY29sdW1u
cw0KICAgICAgciAlPSBAbGluZXMNCiAgICBlbmQNCiAgZW5kDQoNCiAgZGVmIHNldF9mcm9tX3N0
ciBzdHINCiAgICBAdmFsdWVzID0gW10NCiAgICBzdHIuc3RyaXAuc3BsaXQoIlxuIikuZWFjaCBk
bw0KICAgICAgfCBsaW5lX3N0ciB8DQogICAgICBAdmFsdWVzIDw8IFtdDQogICAgICBsaW5lX3N0
ci5lYWNoX2J5dGUgZG8NCiAgICAgICAgIHwgY2hhciB8DQogICAgICAgICBAdmFsdWVzLmxhc3Qg
PDwgY2FzZSBjaGFyLmNocg0KICAgICAgICAgICAgICAgICAgICAgICAgICB3aGVuIElDRS50b19z
DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgSUNFDQogICAgICAgICAgICAgICAgICAgICAg
ICAgIHdoZW4gVkFDVVVNLnRvX3MNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBWQUNVVU0N
CiAgICAgICAgICAgICAgICAgICAgICAgICAgd2hlbiBWQVBPUi50b19zDQogICAgICAgICAgICAg
ICAgICAgICAgICAgICAgVkFQT1INCiAgICAgICAgICAgICAgICAgICAgICAgICBlbmQNCg0KICAg
ICAgZW5kDQogICAgZW5kDQogIGVuZA0KDQogIGRlZiBzdGFydF9zaW0NCiAgICB1bnRpbCBub19t
b3JlX3ZhcG9yPyBkbw0KICAgICAgdGljaw0KICAgICAgd3JpdGUNCiAgICBlbmQNCiAgZW5kICMg
ZGVmIHN0YXJ0X3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