which is more efficient?

0 favourites
  • 5 posts
  • Say i have 100 characters and i'm doing an finite state machine for controling their behaviours, which would be more efficient?

    Monitoring all chars in one big loop and checking all states in one go like this

    foreach char

    state = A, do action

    state = B, do action

    state = C, do action

    or

    picking the chars by states and doing small loops to control each one like this

    state = A

    foreach char, do action

    state = B

    foreach char, do action

    state = C

    foreach char, do action

    Thanks.

  • If there is no total 'idle' state, it does not matter.

    I mean ... if each object for sure has state A, B, or C, needing actions for each state.

    If you really want to squeeze out the last possible iteration, then use Browser object to log an (local variable) iteration counter.

    But, watch the logic. The 'foreach char' is not always needed, when things are not personal.

    Example.

    foreach char < ------- unnecessary

    state = A (is dead)

    ____destroy

    state = A (is dead)

    foreach char < ------- unnecessary

    __destroy

  • Profile it with wallclocktime. Basically you can do this to measure how long some events take. Then you can try both ways and compare it.

    global number starttime=0

    every tick

    --- set starttime to wallclocktime

    // events to profile

    --- set text to wallclocktime-starttime

  • One side tip: you don't need the "for each" in any of the two alternatives. C2 does a hidden "for each" if you just use the condition state.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • In other words, both alternatives (without the "For each" event") are the same.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)