for-loopgml

How often will a condition be calculated in a for loop?


I iterate over a huge ds_list and want to know if the condition (in this case the size of the ds_list) will be calculated after every loop again and again (which would be bad for the performance). In that case I would store the result in a temporary variable.

for (var i = 0; i < ds_list_size(huge_list); i++) {
    // doing something
}

Solution

  • A for loop will run the "condition" expression on each iteration, meaning that it can be beneficial to store the list size in a temporary variable if it will not change during the loop. You may verify this by using a script with a show_debug_message call in it as a condition.