Variable value assignment speed

Posted by Kevnuke on Thu 16 Aug 2018 05:55 AM — 5 posts, 19,810 views.

USA #0
I'm not really sure how to ask this. I'm updating my curing system for Achaea. and since it needs so much work anyway, I figured I may as well update other things too.

Is a boolean value faster to assign or compare to something else than integers? I might change my arrays to hold values of 1 or 0 instead of true/false if it's more efficient. Also, some afflictions can be have up to 8 (I think) levels, so those might have to be integer assignments anyway. For example, aff.wristfractures = 4 (up to 8), if I get hit with an attack enough times for my wrists to be fractured 4 times.

Does the logic in Lua see integer 1 and boolean true as equal? Like:


if 1 == true then
  Note ("True")
end
Australia Forum Administrator #1
Lua is quite fast so I wouldn't fiddle with minor stuff like that. The mapper I wrote draws dozens of rooms (each requiring lots of calculations) in a fraction of a second.

I doubt there is much difference in assigning a number of a boolean (or indeed, a string) because internally they are held as 4 byte fields.

Quote:

Does the logic in Lua see integer 1 and boolean true as equal? Like:


if 1 == true then
  Note ("True")
end




Lua sees nil and false as false and everything else as true. So, for example, an empty string, or the number 0 are true.

Anyway, you can test that sort of thing for yourself.

The number 1 (a floating point number) and the value true (a boolean) will not compare equal.

To write an "if" test for true just write:


if true then
  Note ("True")
end


Or, for example:


if a > 5 then
  Note ("a is greater than 5")
end


Comparisons will return a boolean (so "a == 5") would resolve to either true or false.

However just because you can write:


if 42 then
  print "True"
end

if 666 then
  print "True"
end


That does not mean that 42 is equal to 666, although they are both considered to be true values in an "if" test (because neither of them are nil or false).
Amended on Thu 16 Aug 2018 06:13 AM by Nick Gammon
Australia Forum Administrator #2
Even this will print false:


print (nil == false)


They are both considered false but they are different to each other.
USA #3
I suppose nil is a lack of any value and boolean false is a deliberate value of false.

Thank you for the clarification
USA Global Moderator #4
Quote:
Is a boolean value faster to assign or compare to something else than integers?

I doubt that you're doing enough comparisons and assignments for it to matter. In my experience, the main things to optimize are screen drawing and sqlite access. Everything else may as well be instantaneous.