Consider this code:
$fullText = $beforeStore . $store . $afterStore;
If $store
is big and one wants to optimize memory usage (memory_get_peak_usage
), they will find that here they use thrice the memory needed to keep $store
since $store . $afterStore
is another value of that size (or bigger) as well as $beforeStore . $store . $afterStore
. So to optimize, I used
$fullText = $beforeStore . $store;
unset($store);
$fullText = $fullText . $afterStore;
which gives only twice in terms of memory_get_peak_usage
and the final state (memory_get_usage
) is the same as the initial (I omit unset
ing $beforeStore
and $afterStore
here).
Is there some smart way to concatenate in this optimized manner but without writing 1+n lines of code where n is the number of concatenated strings? (or 2n if we want to unset each of the concatenated lines)