Why optimization is important.

Open link in next tab

Compiler Explorer - C

https://godbolt.org/z/qKjjf5Eq8

/* Type your code here, or load an example. */ int square(int num) { char rv[num][num]; return sizeof(rv); } int cube(int num) { char rv[num][num][num]; return sizeof(rv); } int main(int argc, char** arg) { for (int a = 0; a < 1000; a++) { printf("%d\t%d\t%d\n", a, square(a), cube(a)); } }

Compiler Explorer - C

If you use a compiled language, you should periodically look at Godbolt and see what your code is doing and what changes to your code will do in the compiled output.

In this case a positively insane way of calculating squares and cubes generates 311 lines of ARM assembler output that will swallow your memory. With even something as simple as -O1 on the command line it's replaced by one or two multiplications respectively. With -fwhole-program it removes the functions entirely and interlaces them into the loop in main().

Know your tools. It makes huge differences!