Test for condition or just do it. Which is faster?

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • Lets say you have 100 enemies and you want to find the highest health of all of them.

    You can achieve this two ways:

    Start loop "find highest health" number of enemies times

    On loop "find highest health"
    - ID of Enemy = loopindex("find highest health")
    - Health(Enemy) > Global Value A
    Set Global Value A to Health(Enemy)

    or

    On loop "find highest health"
    - ID of Enemy = loopindex("find highest health")
    Set Global Value A to max(Health(Enemy), Global Value A)

    Obviously as this loop can scale up depending on the number of enemies, I want it to be as efficient as possible. So is it faster to test for a condition or just always set a value.

    Please login to see this link.


  • The fastest way is to use a ForEach loop for this instead of a fastloop. ;)

    My Please login to see this link. (which I actually use), my Please login to see this link. (which I mostly don't use), and my Please login to see this link. (which I don't use anymore pretty much at all really). If there are awards for "'highest number of long forum posts", then I'd have probably won at least 1 by now. XD

  • happygreenfrog I'm very aware of this but this isn't a question about foreach vs fastloop, i'm actually not interested in foreach and i'm avoiding that extension due to instability demonstrated by that extension throughout it's history.

    Last I heard that extension is still unstable and unpredictable.

    And also this is strictly a hypothetical question, in my case I need to favor stability over speed, I only have a maximum 31 objects to loop through, and my program needs to run for days on end without crashing. I can't risk any instability caused by unnecessary 3rd party extensions.

    Please login to see this link.

    Edited once, last by Ryan (August 23, 2013 at 3:09 AM).

  • happygreenfrog I'm very aware of this but this isn't a question about foreach vs fastloop, i'm actually not interested in foreach and i'm avoiding that extension due to instability demonstrated by that extension throughout it's history.

    Last I heard that extension is still unstable and unpredictable.

    And also this is strictly a hypothetical question, in my case I need to favor stability over speed, I only have a maximum 31 objects to loop through, and my program needs to run for days on end without crashing. I can't risk any instability caused by unnecessary 3rd party extensions.


    Instability? Only in some runtimes other than EXE, such as flash, where it recently had to be re-worked. And also, ForEach is far from unnecessary. It's perhaps one of the most essential 3rd party extensions out there. Oh, and did I mention it was made by a mod/admin (he was an admin at one point, I think he was one at the time the extension was released, but he seems to be a mod now). It also seems that Clickteam has the source code, considering the fact that they continue to update it for runtime other than EXE (where it already has anything you could need, want, or even reasonably think of for the purpose of fixing object selection). If anything, it's the fastloops that are going to cause it to crash, because of excessive lag. Even in the runtimes that ForEach is buggy in, it doesn't cause it to crash to my knowledge...

    My Please login to see this link. (which I actually use), my Please login to see this link. (which I mostly don't use), and my Please login to see this link. (which I don't use anymore pretty much at all really). If there are awards for "'highest number of long forum posts", then I'd have probably won at least 1 by now. XD

  • It's true the bugs may have been ironed out, I still prefer Fast Loop in this case because it has a longer history of stability and reliability. Fast loops only cause crashes when used in the wrong way... I've been using them for over 5 years, so I know the exact result of poorly thought-out fast loops. In this context I will rely on something I know works rather than gamble with something that's caused headaches in the past... I've been using ForEach since it first came out too so I have experience with both.

    I must reiterate that I'm only looping through 31 objects so I prefer stability over speed... I usually switch to ForEach for large scale looping, it is a great extension and life saver for large scale games. I've just had a few negative experiences due to the buggy release.

    For my next project I might try to rely on ForEach a bit more, what you're saying might be correct. It may be easy to substitute in and have no negative consequences. When I started this project it wasn't the case. I'll look into it and report back.

    Please login to see this link.

    Edited 3 times, last by Ryan (August 23, 2013 at 4:03 AM).

  • To answer your original question, I think it is faster to always set values to x instead of comparing the condition and then triggering the action, and it's also a bit more efficient in terms of coding (you can make an always event with a bunch of actions instead of a lot of conditions that compare and trigger things).

    Obviously, if the action is relatively complex, it's probably better to compare a condition first.

    Please login to see this link. - Metroidvania RPG (Win/OSX/linux & PS4/Vita/Wii U)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!