lua-users home
lua-l archive

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

> Although 'local' does make a nice difference with while,
> it doesn't really with this for example. Even with the
> x=i, it is pretty much the same speed as without, and
> still plenty faster than while...

> However, I would like to know why... what does it mean?
> And what is the rationale behind it especially when it
> makes things cryptic for the non-programmer users...

Maybe it is cryptic. The simple explanation is that a "local" variable
only exists in the piece of program it occurs in. Since Lua knows where all
the uses of the local variable are, it can find them much more easily.

For example, the "i" in the for loop is local to the for loop; it doesn't
outside the for loop. This is what makes for loops so fast (or one of the
that makes them so fast, anyway). So changing that would make for loops

By the way, i = F(i) is not allowed. You cannot change the value of the
index of
a for loop inside the for loop. I suppose it works for you because F(i)
returns i.

>  local x=-1
>  for i=1,10000000 do
>    i = F(i)
>    x = i
>  end

> That's too much for users I think, is there any solutions
> that makes sense, which is not twice as slow like 'while' ?

Not really. Sorry.