NOTE: Already made this a Wiki. I don't care what this question is tagged as, as long as there is a good discussion.
I've heard that since in pure functional programs, there are no side effects and values dont mutate, it makes it easier for the compiler to make more runtime optimizations. To what extent is this true?
If this is true, my next concern is what is the loss in freedom that we are trading this for? I mean, in languages like C++/C the developer is totally in control and can tweak a lot of things. If we give away this job to the compiler, we lose that opportunity. The bright side of this is that even a non-expert programmer can write good code. Furthermore, these days with so many layers of cache in the machine architecture, may be even an expert cannot really do anything worthwhile. So, delegating this job to the compiler that knows more about the underlying architecture than the programmer does, is a good idea.
What are your suggestions?
Have you seen this "good math, bad math" blog post?
I program in C, assembly and OCaml. I sometimes look at the assembly generated by the C compiler. It is dreafully inefficient. Often I notice that a global variable is re-loaded again and again, when it's clear (to someone aware of the logic of the program) that it doesn't change during the execution of the function, but because the function manipulates pointers, the compiler cannot assume that the variable isn't touched. One simple way to fix this when you notice it is to copy the contents of the global into a local at the beginning of the function, this way you keep the portability of C but get assembly-like performance...
But you shouldn't have to do this, and indeed, with a higher-level language, you don't have too, because there are less pointer, casts and other low-level constructs that let you get close to the hardware when you want to but actually get in the way of automatic optimization.
there are no side effects and values dont mutate, it makes it easier for the compiler to make more runtime optimizations
In a sense, yes - until you realize that side-effects and mutable values are optimizations themselves.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With