Re: Do you use a garbage collector (java vs c++ difference in
=?UTF-8?B?Im5ldyIp?=
This is a MIME GnuPG-signed message. If you see this text, it means that
your E-mail or Usenet software does not support MIME signed messages.
The Internet standard for MIME PGP messages, RFC 2015, was published in 1996.
To open this message correctly you will need to install E-mail or Usenet
software that supports modern Internet standards.
--=_mimegpg-commodore.email-scan.com-4469-1207880983-0007
Content-Type: text/plain; format=flowed; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Razii writes:
On Thu, 10 Apr 2008 21:43:45 -0400, Arne Vajh=C3=B8j <arne@vajhoej.dk>
wrote:
I can not imagine any C++ runtime that makes an operating system
call for each new.
The runtime allocates huge chunks from the OS and then manage
it internally.
Testing the keyword "new"
Time: 2125 ms (C++)
Time: 328 ms (java)
Explain that. What I am doing different in java than in c++? Code
below..
What you're doing different in Java is that you're using the part of the
language that it's optimized for. Java is, generally, optimized for fast
instantiation of discrete objects on the heap, because creating a huge
number of objects is unavoidable in Java, and they all have to be allocat=
ed
on the heap, due to the nature of the language itself.
On the other case, in most use cases C++ does not require instantiation o=
f
as many discrete objects on the heap that Java does, for implementing an
equivalent task. In C++, most -- if not all -- of these objects can be
easily allocated on the stack.
So, if you were to do a fair comparison, you should benchmark instantiati=
on
of Java objects, on the heap, against instantiation of C++ objects on the
stack.
And then benchmark the comparable memory usage, to boot :-)
In your C++ example, there was no reason whatsoever to instantiate your C=
++
test object on the heap. What does that accomplish, besides a memory leak=
?
That's just the Java way of doing things, but, in C++ you have the option =
of
instantiating objects on the stack, so try benchmarking this instead:
int main(int argc, char *argv[]) {
=09clock_t start=clock();
for (int i=0; i<=10000000; i++)
{
Test testObj(i);
if (i % 5000000 == 0)
cout << &testObj << endl;
}
clock_t endt=clock();
std::cout <<"Time: " <<
double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
}
See how well /that/ benchmarks against Java :-)
--=_mimegpg-commodore.email-scan.com-4469-1207880983-0007
Content-Type: application/pgp-signature
Content-Transfer-Encoding: 7bit
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
iD8DBQBH/s0Xx9p3GYHlUOIRAvBpAJ9++ag+arLm2Tw7MUDh1C9RseFvCACdG01b
bJVz24MY+1330SyZGmIWYbk=
=5Qsh
-----END PGP SIGNATURE-----
--=_mimegpg-commodore.email-scan.com-4469-1207880983-0007--