Efficiency of immutable vs mutable

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 2 22:17:30 PST 2015


On Tuesday, November 03, 2015 03:16:06 Andrew via Digitalmars-d-learn wrote:
> I've written a short D program that involves many lookups into a
> static array. When I make the array immutable the program runs
> faster.  This must mean that immutable is more than a restriction
> on access, it must affect the compiler output. But why and how?

If a variable is immutable, then the compiler knows that it will never
change, and at least some of the time, the compiler is able to use that
information to better optimize the code. But exactly how much the compiler
is able to optimize based on immutable is going to be very dependent on the
code in question and on how the compiler's optimizer works.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list