lua-users home
lua-l archive

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


on 2/21/08 7:15 AM, Hans Hagen at pragma@wxs.nl wrote:

> actually,
> 
>      tab[char] = (count or 0) + 1
> 
> is slower than
> 
>      if count then
>          tab[char] = count + 1
>      else
>          tab[char] = 1
>      end
> 
> (the if then variant takes 60% of the time of the or case; the
> interpreter needs an extra LOADK)
> 
> in a similar fashion, parallel assignments can be faster

That seems surprising. Admittedly for a small input set, we avoid doing the
addition but I would expect in a long running case, we're mostly on the
first branch of the if and since or is short-circuiting it should amount to
essentially the same computation.

My guess, however, would be that the __index approach is faster in the long
run because while it makes the initialization case more expensive (it
requires a Lua function call), it does the detection as part of the standard
table fetch logic.

Mark