SKKmods Posted June 1, 2018 Posted June 1, 2018 With a bunch of complex AND logical tests in a single If statement, does the papyrus script engine bail on the first test fail, or process the whole line, so better to nest them ? Neat for code maintenance: If (Simple logical test A) && (Simple logical test B) && (Simple logical test C) && (Complex array lookup logical test D) ;do stuffEndIf Better for performance ? If (Simple logical test A) If (Simple logical test B) if (Simple logical test C) If (Complex array lookup logical test D) ;do stuff Endif Endif EndifEndif Or halfway split simple and complex ? If (Simple logical test A) && (Simple logical test B) && (Simple logical test C) If (Complex array lookup logical test D) ;do stuff EndifEndif Because performance is a virtue.
Reneer Posted June 1, 2018 Posted June 1, 2018 (edited) I'm pretty sure the compiler takes care of that - if the first condition fails the others are not evaluated. There is a Papyrus performance page on the CK Wiki: https://www.creationkit.com/fallout4/index.php?title=Performance_(Papyrus)#Fail_fast.2C_fail_early Edited June 1, 2018 by Reneer
pra Posted June 1, 2018 Posted June 1, 2018 I'm pretty sure the compiler takes care of that - if the first condition fails the others are not evaluated. There is a Papyrus performance page on the CK Wiki: https://www.creationkit.com/fallout4/index.php?title=Performance_(Papyrus)#Fail_fast.2C_fail_earlyIt's not the compiler, but the interpreter/vm/whatever you want to call the part which actually runs the .pex files </nitpick> I think it can be safely assumed that most programming languages are able to do this sort of short-circuiting, but thank you for confirming it nonetheless. That said, I sometimes make several ifs with return statements instead of one single one for the sake of readability. But also if I need a variable for one of the checks: function doStuff(ObjectReference someRef) if(!someRef) return endif ObjectReference player = Game.GetPlayer() if(someFunction(player, someRef) = 0) return endif ... endfunction
SKKmods Posted June 1, 2018 Author Posted June 1, 2018 Thanks Foxy, with the lack of proper sdk specs and docs, optimisations are rather opaque. I shall find some time to setup a benchmark for this, a useful investment in a (probably) sunset platform ... @pra I try and avoid using return as that just leads to me creating complex nested chains of recursive functions that stack and crash.
pra Posted June 1, 2018 Posted June 1, 2018 @pra I try and avoid using return as that just leads to me creating complex nested chains of recursive functions that stack and crash.Wat? How? I always try to return as soon as possible. If I know that the function can't do anything sensible anymore, I make it return right away. The alternative would be to create complex nested if/else constructs, and having an extra variable for the return value, if any. Which I find incredibly ugly and hate Pascal (fo4edit scripting language) for trying to enforce it.
SKKmods Posted June 1, 2018 Author Posted June 1, 2018 I'm talking papyrus. not using return forces me to chunk my functions and use events rather than create thousand line monster functions. its probably philosophical ...
Evangela Posted June 1, 2018 Posted June 1, 2018 (edited) I personally find the halfway one to get real confusing when it's more than 2 && conditions, with each check being a condition in itself(A > B && A < D && C > D etc..). If chains I can see a "staircase" clearly and not get lost. If there were switches though, I'd probably never need a long if chain. Maybe in the next game.. Edited June 1, 2018 by Rasikko
Reneer Posted June 1, 2018 Posted June 1, 2018 I'm pretty sure the compiler takes care of that - if the first condition fails the others are not evaluated. There is a Papyrus performance page on the CK Wiki: https://www.creationkit.com/fallout4/index.php?title=Performance_(Papyrus)#Fail_fast.2C_fail_earlyIt's not the compiler, but the interpreter/vm/whatever you want to call the part which actually runs the .pex files </nitpick> True enough, since the compiler can't exactly foresee what the variables are going to be at runtime. :tongue:
FatsackTony1 Posted June 3, 2018 Posted June 3, 2018 Every language I know short circuits as soon as the first false is detected when you're comparating a bunch of and statements.
pra Posted June 4, 2018 Posted June 4, 2018 I'm pretty sure the compiler takes care of that - if the first condition fails the others are not evaluated. There is a Papyrus performance page on the CK Wiki: https://www.creationkit.com/fallout4/index.php?title=Performance_(Papyrus)#Fail_fast.2C_fail_earlyIt's not the compiler, but the interpreter/vm/whatever you want to call the part which actually runs the .pex files </nitpick> True enough, since the compiler can't exactly foresee what the variables are going to be at runtime. :tongue: It could do compile-time optimisation, though. For example, if you literally write "if (foobar && true)" it could optimize the second part away. A more advanced compiler could also understand something like "bool foo = true; if(foo)". But I doubt this bethesda compiler could do anything like this.
Recommended Posts