Re: Short-lived Objects - good or bad?
Andreas Leitgeb wrote:
Arne VajhHj <arne@vajhoej.dk> wrote:
I would be very surprised if effective GC of short lived objects
would make GC of 1 long lived object more expensive than of 1000
short lived objects.
That might be the position of the reviewer, but I've been told
other times, that allocation/gc costs are generally neglectible
compared to (re-)initialisation costs (the constructor). In case
of an allocation the fields are nullified en bloc, upon re-init
they'd be nullified one by one.
Exceptions exist, and have been mentioned already.
The fact that allocation and GC are very cheap has nothing to do
with what I wrote.
t1 = cost of allocating one foobar
t2 = cost of GC one short lived foobar
t3 = cost of GC one longlived foobar
t4 = cost of initializing a foobar (constructor or otherwise)
t5 = loop overhead per iteration
t6 = actually work per iteration
outside:
cost = t1 + 1000 *(t4 + t5 + t6) + t3
inside:
cost = 1000 * (t1 + t4 + t5 + t6 + t2)
outside - inside = -999*t1 -1000*t2 + t3
My postulate was that -1000*t2+t3 was negative making the
difference negative.
The fact that t1+t2 is smaller than t4 does not disprove that.
The fact that t1+t2 is much smaller than t5+t6 proves that
outside/inside is very close to 1.
Which is the argument for write the code that are most natural.
Arne