In general, Pure functions have 3 advantages over unclean functions that the compiler can use:
Caching
Suppose you have a pure function f , which is called 100,000 times, since it is deterministic and depends only on its parameters, the compiler can calculate its value once and use it if necessary
Parallelism
Pure functions are not read or written to any shared memory and therefore can be run in separate threads without any unexpected consequences.
Pass by link
The function f(struct t) takes its argument t by value, and on the other hand, the compiler can pass t reference to f if it is declared as clean, ensuring that the value of t does not change and will get a performance gain
In addition to compile-time considerations, pure functions can be tested quite simply: just call them.
No need to create objects or connect to a DB / file system.
Uri Goren May 13 '15 at 18:53 2015-05-13 18:53
source share