lua-users home
lua-l archive

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


Klaus Ripke <paul-lua@malete.org> wrote:
(28/02/2005 18:27)

>
>On Monday 28 February 2005 19:07, David Olofson wrote:
>> > In a case like this, I use this:
>> > 
>> >  y = 2
>> >  if a then  y = 1  end
>> >
>> > Only two, no 'else' and no chance of a var depending on a
>> > conditional branch merely for its prior existence...
>>
>> Right, that's the universal solution; works with pretty much every
>> language. The only problem is that it's kind of wasteful if the
>> assignment is more expensive than loading a value into a register or
>> similar.
>tests showed this about equally fast as if..y=..else y=,
>if a is false, and about 10% slower, if a is true
>(all variables being local)


ok, but if I was using it once or rarely, this is going to be of minuscule significance. :)

On the other hand, if there were to be a large recurring test I might make the first use outside of it, and use "if a then y=1 else y=2 end" so that there is just one assignment in each pass. While the third (outside) instance, prior to the loop, might look superficially ugly, it's not that bad I think, and if you saw it, you'd know why it was there, if you are coding with speed in mind.

I don't know if the 'local' bit also makes >=10% extra work per initial assignment, but if it does we have not avoided the need for an outside prior instance to set it up for speed even as Lua is now written, but we could avoid having to say 'local' in order to avoid breakages due to conditional assignments once the prior assignment/declaration exists..