Re: Do you use a garbage collector (java vs c++ difference in "new")
Razii wrote:
On Thu, 10 Apr 2008 20:37:59 -0500, Razii
<DONTwhatevere3e@hotmail.com> wrote:
int main(int argc, char *argv[]) {
clock_t start=clock();
for (int i=0; i<=10000000; i++) {
Test *test = new Test(i);
if (i % 5000000 == 0)
cout << test;
}
If I add delete test; to this loop it gets faster. huh? what the
exaplanation for this?
2156 ms
and after I add delete test; to the loop
1781 ms
why is that?
Due to caching at various levels of the memory hierarchy, accesses to
recently referenced virtual addresses are often a lot faster than
accesses to new ones. The original C++ code requested 10,000,000
distinct Test-sized memory allocations with no reuse. With "delete
test;" the memory allocator can reissue the same piece of memory for
each "new" operation.
In addition, the sheer amount of memory being allocated in the original
C++ program may have required some system calls to get additional
allocatable memory.
The JVM is free to reuse the virtual memory previously occupied by an
unreachable Test object, so the version with "delete test;" is a bit
more comparable to the Java program.
This illustrates the basic problem with snippet benchmarks. In modern
computers the performance of small operations depends on their context.
Taking them out of context is not realistic.
Patricia