lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 10/03/2010, at 12:29 PM, Mike Pall wrote:
> Geoff Leyland wrote:
>> I'm not quite sure I understand how the shootout statistics
>> work, and it's a bit  puerile to game benchmarks, but you can
>> make nbody about 9% faster by using numeric rather than string
>> indexes for the planet data.  Unfortunately, I think that to
>> make a difference you'd need to get it under the median, maybe
>> requiring a 30% improvement.
> 
> It's explicitly labeled as a benchmark on struct access. Numeric
> indexes won't qualify. But the low-level binary structs I plan to
> add would qualify -- the inner loops are identical, only the
> initialization changes.

As far as I can tell, the Python and Erlang versions of nbody use something more like numeric indexes than field names.
This (possibly non-confirming but field name using) version of advance seems to be about 6% better on nbody.  But it won't move the damn median!

That's probably enough silliness from me about this :-)

Cheers,
Geoff

local function advance(bodies, nbody, dt)
  for i=1,nbody do
    local bi = bodies[i]
    local bix, biy, biz, bimass = bi.x, bi.y, bi.z, bi.mass
    local bivx, bivy, bivz = bi.vx, bi.vy, bi.vz
    for j=i+1,nbody do
      local bj = bodies[j]
      local dx, dy, dz = bix-bj.x, biy-bj.y, biz-bj.z
      local mag = sqrt(dx*dx + dy*dy + dz*dz)
      mag = dt / (mag * mag * mag)
      local bm = bj.mass*mag
      bivx = bivx - (dx * bm)
      bivy = bivy - (dy * bm)
      bivz = bivz - (dz * bm)
      bm = bimass*mag
      bj.vx = bj.vx + (dx * bm)
      bj.vy = bj.vy + (dy * bm)
      bj.vz = bj.vz + (dz * bm)
    end
    bi.vx = bivx
    bi.vy = bivy
    bi.vz = bivz
    bi.x = bix + dt * bivx
    bi.y = biy + dt * bivy
    bi.z = biz + dt * bivz
  end
end