if(!x) is slower than if(x){}else
Numbered Steps to Reproduce Problem:
Code Snippet (if applicable) to Reproduce Problem:
/proc/main()
var/count = 0
var/n = null
var/x = 0
// your setup code here
sleep(1)
var/start = world.tick_usage
while (world.tick_usage - start < 100)
count += 1
// Uncomment whichever you want to test
// if (n) {} else { x++; }
// if (!n) { x++; }
world.log << count
Expected Results:
These should be equivalent in speed, as they are equivalent in functionality. While it's not surprising that `isnull` would be faster than `!`, these are identical in behavior.
Actual Results:
if(n){} is consistently about 10% faster.
Equivalent testing gives results of 4,994,761 iterations with the inverted-if, and 4,559,987 iterations with isnull when the timescale is in a full second.
They are not equivalent in functionality, unless you are expecting the compiler to optimize your code for you.
if(!n) {x++;}
Not BYOND's exact bytecode, but the naive compilation would have an extra operation, push and pop for the !n case, while the n case would just have a jump. As jumps would be implemented as numeric offsets in most virtual machines, they'd be less costly than just about anything else.
Modern programming languages might have optimization that look for cases with redundant branching, and optimize them into the simpler pattern. DM has very little of this kind of optimization. A modern programming language would offer you a different pattern than the one you wrote. DM hardly ever does that.