I just started using RPGM MV and i am trying to figure out if is better to simulate AND/OR with conditional branches or better to just script it performance wise, quick example i need to make a bingo, while extracting a number between 1 and 100 i need to never repeat an already extracted number, i could easily use variables to contain every previous extraction and compare all of them with every new extraction using that many IF to repeat it until a different number is extracted, but that seems to become a mess of too many IF when i need to do for example 50 extractions in a row, should i use as much as possible scripts and forget about simulating AND/OR with conditional branches or there is an actual merit on using them for maybe smaller scale comparisons?
Performance question about simulating AND/OR with conditional branches
Game Mechanics DesignAny difference in processing speed is going to be so negligible as to be unnoticeable on all but the most potato PC.
However, for something like this what I would do is set up a 101-element array mapped to each element's index + 1, then have a loop that splices a random element from the array until it's empty.
However, for something like this what I would do is set up a 101-element array mapped to each element's index + 1, then have a loop that splices a random element from the array until it's empty.
there is a snippet on this forum how to do that from a variable
and have number 1-100 in the array.
the part that uses $gameVariables is a snippet on the forum on how to do that,
the top part is also a snippet but I just adjusted it to get the variable
array with 1-100 like you want, s you can use it in a event.
just change the variableID's
and have number 1-100 in the array.
JavaScript:
========================================
Draw number from array to use only once
========================================
let nums = [1,2,3,4,5,6,7,...100] (up to 100)
or start with:
let nums= [];
for (let i = 1; i <= 100; i++) {
nums.push(i) }
$gameVariables.setValue(453, nums);
$gameVariables.setValue(454, $gameVariables.value(453).splice(Math.floor(Math.random() * $gameVariables.value(453).length), 1))
var arr = $gameVariables.value(453); // array of values
var n = arr.indexOf(12);
if(r >= 0) {
var r = Math.randomInt(Math.min(n +1, arr.length) - 1); // random index
$gameVariables.setValue(454, arr.splice(r, 1));
}
the part that uses $gameVariables is a snippet on the forum on how to do that,
the top part is also a snippet but I just adjusted it to get the variable
array with 1-100 like you want, s you can use it in a event.
just change the variableID's
You can also start with
JavaScript:
let nums = Array.from({length: 101}, (value, key) => key + 1);
so... a scrambler block?
if that is ES6 coding, or a different way, I dont know that way, I do a mix of ES5 and ES6,
but I dont know if indexOf(12) is also correct or need to match the number.
but there are many ways, depending what works best, I like for..loop, but if it's only numbers,
than I would try to start like Trihan suggested as it is a 1 liner and readable (if you know JS).
but I dont know if indexOf(12) is also correct or need to match the number.
but there are many ways, depending what works best, I like for..loop, but if it's only numbers,
than I would try to start like Trihan suggested as it is a 1 liner and readable (if you know JS).
Yes that was the example used for the question, the answer seems to be that there is almost no noticeable difference on a simulated AND/OR with conditional branches and via script
The true answer is that it depends on how lucky you are with RNG, and the more numbers have been drawn the longer overall it will take to get a valid one using the "store drawn numbers and redraw if trying to draw an existing one" approach. It's theoretically possible to continually draw an already-drawn number to the point where there's noticeable delay.
Well yea. That was the realistic scenario that came to mind to trigger many conditional branches in comparison with just scripted AND, maybe i shouldnt have said the example that way since that was not the main topic of the question.
shouldn't it be possible to get the array "let nums = []" to push 1-100 into it and drawed number
-1 inside nums array into a new array: numsDrawed = [] and store it there?
so you can check if it exist and the nums array become shorter while numsDrawed become bigger?
-1 inside nums array into a new array: numsDrawed = [] and store it there?
so you can check if it exist and the nums array become shorter while numsDrawed become bigger?
If you start with the fully-populated array, there's no need for a numsDrawed at all.
In general, if you feel like an algorithm is going to require you to make enough conditional branches that you're concerned about this, it's probably safe to assume that there's a better way to go about it. In the case of your example problem, the most performant method would probably be to create an array containing one of each number, shuffle it, and then simply pop a number off the end every time you need one:
The splicing method works, but it would likely be slower, because it has to do a lot of superfluous memory operations (due to having to shift the remaining elements of the array every time you draw a number).
But if the example itself is irrelevant, and you're moreso just wondering whether a conditional branch event command is slower than a JavaScript conditional statement, then the answer is technically "yes", but not enough that it should realistically matter.
JavaScript:
// Fisher-Yates Shuffle Algorithm
function shuffle(array) {
let currentIndex = array.length, temporaryValue, randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// Generate an array of numbers from 1 to 100
let numbers = Array.from({ length: 100 }, (_, index) => index + 1);
// Shuffle the array
numbers = shuffle(numbers);
while (numbers.length > 0) {
// Pop the last element from the array
let number = numbers.pop();
// Do something with the number
console.log(number);
}
The splicing method works, but it would likely be slower, because it has to do a lot of superfluous memory operations (due to having to shift the remaining elements of the array every time you draw a number).
But if the example itself is irrelevant, and you're moreso just wondering whether a conditional branch event command is slower than a JavaScript conditional statement, then the answer is technically "yes", but not enough that it should realistically matter.
simply:
no condition, no nothing.
Code:
ary1 = [1..10]
loop
index = rand(ary1.length)-1
ary2.push(ary1.pop(index))
until ary1.length == 0
no condition, no nothing.
Thanks a lot for the answer, yes i was very curious if there was a clear difference on performance or not
No problem. If you use the event command, then the engine has to interpret the command, convert it into a JavaScript statement, and then execute said JavaScript statement. So it's definitely faster to just cut out the middle man, and supply a JavaScript statement from the get-go. However, we're probably talking fractions of a millisecond, so I can't think of many realistic scenarios where that fact should really matter.
Wait, what? Caethyril said the exact opposite here. So who is right?
Post in thread 'Caethyril's MZ Plugins' https://forums.rpgmakerweb.com/index.php?threads/caethyrils-mz-plugins.125657/post-1373818
I haven't tried to run any benchmark tests, and @Arthran absolutely knows more than I do.
But everything I've read says that eval() is a slow process, which is what all Script fields in the editor use.
So I think it ultimately depends on the usage - if you have a lengthy Script command that's filled with a number of commands/loops/stuff, it will probably balance out as being more efficient. But to just use the Script version of any of the basic conditional branch commands I would bet is slower than the engine simply processing the event command.
The list of event commands are processed into their code functions once when the map is loaded, it's not like the engine has to translate every command from the JSON into JavaScript every time. Whereas using the Script field would get that slower execution added in every time the conditional branch is evaluated.
Tell me if I'm wrong 😉
@ATT_Turan all things being equal, it takes one extra go for the processor to load both operands to it's own registry and compare them, than it takes to access one position of memory and write to it.
it's one extra instruction, in a million per second.
it's negligible.
it's one extra instruction, in a million per second.
it's negligible.
But that's not what's happening, right? Because in the Script route it runs it through the eval() function, which first converts the string through the JavaScript interpreter, then actually runs the code.
So it's not just accessing one position of memory. Unless I misunderstand what you're saying.
Technically, our statements actually aren't at odds. My statement was not about the usage of script fields/commands, but very specifically comparing "a conditional branch event command" against "a JavaScript conditional statement". I was simply explaining that the event command turns into a JavaScript conditional statement in the end, but since it has to do some other stuff first, it's technically slower than a pure JavaScript conditional statement. I still maintain that that is a true statement.
But note that I never used the term "Script command" in my statement. I meant "JavaScript conditional statement" in a very literal, isolated sense. Since JavaScript conditional statements don't only exist in script commands (you'll find plenty of them in the core scripts or in plugins), I considered the additional overhead associated with using script commands to be outside the scope of my statement.
In hindsight, I see that I probably should have predicted that people would interpret my statement to be one about the usage of regular event commands vs equivalent script commands, so it's my bad for not being more clear and adding appropriate disclaimers. Since I write a lot of plugins, I didn't really think of it that way, and I was due for a nap at the time, so I mistakenly took it for granted that people would understand my intentions.
But to speak on the event command thing a bit:
If I were to make a statement specifically about the performance difference of using a regular event command vs replicating it with a script command, then I'd say that the regular command will almost always be faster. However if one were to ask about the performance difference of solving a complex problem using regular event commands vs using a script command, that's way too situational to make a blanket statement about.
Original thread: https://forums.rpgmakerweb.com/threads/performance-question-about-simulating-and-or-with-conditional-branches.161660/