Performance question about simulating AND/OR with conditional branches

Derpete

Jester
Member
Joined
Sep 30, 2023
Messages
12
Reaction score
4
First Language
Italian
Primarily Uses
RMMV
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?
 
Last edited:

Trihan

Speedy Scripter
Regular
Joined
Apr 12, 2012
Messages
6,883
Reaction score
7,989
First Language
English
Primarily Uses
RMMZ
Any 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.
 

ShadowDragon

Realist
Regular
Joined
Oct 8, 2018
Messages
8,251
Reaction score
3,523
First Language
Dutch
Primarily Uses
RMMV
there is a snippet on this forum how to do that from a variable
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
 

Trihan

Speedy Scripter
Regular
Joined
Apr 12, 2012
Messages
6,883
Reaction score
7,989
First Language
English
Primarily Uses
RMMZ
You can also start with

JavaScript:
let nums = Array.from({length: 101}, (value, key) => key + 1);
 

gstv87

Regular
Regular
Joined
Oct 20, 2015
Messages
3,369
Reaction score
2,577
First Language
Spanish
Primarily Uses
RMVXA
while extracting a number between 1 and 100 i need to never repeat an already extracted number
so... a scrambler block?
 

ShadowDragon

Realist
Regular
Joined
Oct 8, 2018
Messages
8,251
Reaction score
3,523
First Language
Dutch
Primarily Uses
RMMV
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).
 

Derpete

Jester
Member
Joined
Sep 30, 2023
Messages
12
Reaction score
4
First Language
Italian
Primarily Uses
RMMV
so... a scrambler block?
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
 

Trihan

Speedy Scripter
Regular
Joined
Apr 12, 2012
Messages
6,883
Reaction score
7,989
First Language
English
Primarily Uses
RMMZ
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.
 

Derpete

Jester
Member
Joined
Sep 30, 2023
Messages
12
Reaction score
4
First Language
Italian
Primarily Uses
RMMV
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.
 

ShadowDragon

Realist
Regular
Joined
Oct 8, 2018
Messages
8,251
Reaction score
3,523
First Language
Dutch
Primarily Uses
RMMV
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?
 

Trihan

Speedy Scripter
Regular
Joined
Apr 12, 2012
Messages
6,883
Reaction score
7,989
First Language
English
Primarily Uses
RMMZ
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?
If you start with the fully-populated array, there's no need for a numsDrawed at all.
 

Arthran

Regular
Regular
Joined
Jun 25, 2021
Messages
1,253
Reaction score
1,947
First Language
English
Primarily Uses
RMMZ
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:

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.
 

gstv87

Regular
Regular
Joined
Oct 20, 2015
Messages
3,369
Reaction score
2,577
First Language
Spanish
Primarily Uses
RMVXA
simply:
Code:
ary1 = [1..10]
loop 
  index = rand(ary1.length)-1
  ary2.push(ary1.pop(index))
until ary1.length == 0

no condition, no nothing.
 

Derpete

Jester
Member
Joined
Sep 30, 2023
Messages
12
Reaction score
4
First Language
Italian
Primarily Uses
RMMV
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.
Thanks a lot for the answer, yes i was very curious if there was a clear difference on performance or not
 

Arthran

Regular
Regular
Joined
Jun 25, 2021
Messages
1,253
Reaction score
1,947
First Language
English
Primarily Uses
RMMZ
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.
 

AquaEcho

Script Kitty
Regular
Joined
Sep 20, 2021
Messages
2,280
Reaction score
1,714
First Language
English
Primarily Uses
RMMV
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
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
 

ATT_Turan

Forewarner of the Black Wind
Regular
Joined
Jul 2, 2014
Messages
12,772
Reaction score
11,421
First Language
English
Primarily Uses
RMMV
Wait, what? Caethyril said the exact opposite here. So who is right?
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 :wink:
 

gstv87

Regular
Regular
Joined
Oct 20, 2015
Messages
3,369
Reaction score
2,577
First Language
Spanish
Primarily Uses
RMVXA
@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.
 

ATT_Turan

Forewarner of the Black Wind
Regular
Joined
Jul 2, 2014
Messages
12,772
Reaction score
11,421
First Language
English
Primarily Uses
RMMV
@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.
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.

1696207556964.png
 

Arthran

Regular
Regular
Joined
Jun 25, 2021
Messages
1,253
Reaction score
1,947
First Language
English
Primarily Uses
RMMZ
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
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.
 

Latest Threads

Latest Posts

Latest Profile Posts

I hate driving in games. Imagine your character profile being an exceptional individual, yet when they drive, it makes them look dumb.
The flaming skeleton visited me in my dreams again. He demands the game be finished soon. I don't like the flaming skeleton. He took my cranberry juice. He doesnt even have a stomach it just spilled all over the floor.
My Game Jam entry is a bunch of mini games wrapped within an epic story that will rival the deepest plots of the earliest CRPGs! As a small spoiler, here is one example of a game you will be able to play. Anyone want to try Brat, Krampus, Santa?
so I had two plugin that essentially overwrite each other depending on where they are in the list. so I had to edit what was overwriting one into the other and now they both work. I REALLY need to sit down and just learn JavaScript. I hate that I know just enough to know I don't know NEARLY enough lol.
The Open Hand scandal got even more spicy. The Completionist streamer Jirard made a video response and tossed a bunch of links and documents, but apparently none of it actually addressed why donations he collected sat for 10 years (way too long to justify). He also seemed to threaten defamation suits vs. people for what amounts to pointing out publicly available records. The IRS should definitely audit.

Forum statistics

Threads
136,914
Messages
1,271,398
Members
180,704
Latest member
Kanver47D
Top