All Questions


Fabian Ruch(fabian-ruch)

Constant instead of Pi/3

Hi Nathan,

is it better to use a constant instead of the diction PI/3 or is the calculation time neglectable?

  • Nathan Lovato replied

    One division is negligible. Your computer can do (hundreds of) millions of these every second.

    When it comes to optimization, you want to focus on what you can measure, using the profiler. if you use it, you'll see having a calculation vs a variable makes no measurable difference in your game's execution time: what's happening in the engine and the rest of your code is much more complex.

    Lastly, as far as I remember, the GDScript compiler does constant folding, meaning it pre-calculates constant expressions like PI / 3.0 at compile time.

    In general, you want to use constants not so much for optimization but rather to help you with readability, to name expressions that are hard to read.

  • R
    Fabian Ruch(fabian-ruch) replied

    Thanks, makes a lot of sense 🙂