lua-users home
lua-l archive

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


Hmmm. I actually meant:

tab[char] = (tab[char] or 0) + 1

Although it shouldn't make any difference.

I'm a bit confused though, because I measured:
 for i = 1, 1024*1024*64 do
   test.a = (test.a or 0) + 1
 end
versus:
 for i = 1, 1024*1024*64 do
   local count = test.a
   if count then
     test.a = count + 1
   else
     test.a = 1
   end
 end

And on my compiler/computer at least, the top one is consistently faster. No idea why, given that both give the same opcodes for the main loop:
 Gettable, Test, Add, Settable

Of course, if you mean that the first time through the loop would be faster in the bottom case you would be spot on. Still, if the loop takes any amount of time, I'd figure the first hit for each letter insignificant. Just an imo. In any case the top way is a lot neater. =)

Javier: Mike's way is fastest and neatest =)

- Alex

----- Original Message ----- From: "Hans Hagen" <pragma@wxs.nl>
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

Hans