Question about gdscript: Which is more efficient?

https://sopuli.xyz/post/4120489

Question about gdscript: Which is more efficient? - Sopuli

I’m just curious about which is the most efficient way of doing this (in a loop): o=[var1,var2,var3,varN][i] or match i: 0: o=var1 1: o=var2 2: o=var3 N-1: o=varN or var items = [var1,var2,var3,varN] for i in something(): o=items[i] Or is there a more efficient way of doing it?

  • They don’t do the same thing so I’m assuming you’re trying to iterate over something like in n3
  • n3 should theoretically be the fastest as there’s only one allocation and less branches
  • Time it with get_ticks_usec() over a significant number of samples docs.godotengine.org/en/stable/…/class_time.html#…
  • None of this will matter because GDScript is interpreted and the execution times vary wildly based on how the moon is aligned
  • Premature optimization, yada yada