Manuel Meyer
1 min readNov 28, 2022

--

(also referring to our conversation on LinkedIn)

Basically one gist of your article is: copying the stack is expensive and must be avoided.

My take on it: maintaining mutability also has some cost for the compiler associated with it — I now had a day and created an experimental comparison.

you will find it here: https://gitlab.com/vikingosegundo/comparison/-/blob/main/comparisionIncrease/main.swift

basically all it does is increment a value — once mutable, once immutable (the style that I describe in my articles).

it runs 5 million tests for 10 times

My observations: in by far most of the runs the IMMUTABLE test run between 1 and 4% faster. Sometimes the MUTABLE test was faster, but just up to 1%. This is on a M2 MacBook Air. This also means, that both implementations could be executed >5 million times per second.

I'd conclude, that these results are equivalent — so close that effects like order of function calls or location in memory could have an effect. Compiling for different architecture might have effects.

But: Immutability does not come with the extra costs that many people seem to associate with copying stack values.

--

--

Manuel Meyer
Manuel Meyer

Written by Manuel Meyer

Freelance Software Developer and Code Strategist.

Responses (1)