performance - What are the benefits of `let` in Swift? -
i know swift encourage programmers use constants (let
) instead of variables (var
) everytime makes sense.
this thing because providing more details compiler our code means , compiler can better prevent making mistakes (e.g. changing value should not changed).
my question is, there performance optimizations compiler apply when use constants instead of variables? (e.g. faster executions times, lower footprint, ...).
you asked "...are there performance optimisations compiler apply when use constants instead of variables?"
the answer yes, absolutely.
mutable collections may organized differently immutable ones in order allow them changed. immutable collections can optimized read-only operation.
then there use of mutable/immutable objects. compiler may have generate code copies mutable object when it's shared property of object in order avoid undesired side-effects.
comparison of immutable objects (equatable/comparable) can optimized in ways mutable objects can't.
sulthan's point intelligence of compiler 1 though. compiler can deduce variable never going change code analysis, can make benchmarking let vs. var usage hard.
Comments
Post a Comment