[...]
C++ is not faster than Java in everything. Memory allocation in most
systems is considerably slower in C++ (and C) than in java. (OTOH Java
lives on constant memory allocation due to how the language works, while
most C++ programs don't.) [...]
You don't even have to resort to such dirty trickery to "prove" it.
Just make a program which allocates 10 million small objects dynamically
in both languages, and there you go: You have proven Java to be vastly
faster than C++.
IMVHO, you're assertions are wrong Juha. Take this Java code:
http://pastebin.com/f7361735b
vs this C++ code:
http://pastebin.com/f36011b3b
On my old P4 3.06gz w/ 512mb ram I am getting the following output using
Unix `time' command:
C++ version - gcc (GCC) 3.4.5 (mingw special)
______________________________________________________________________
$ time ./test
real 0m19.766s
user 0m0.015s
sys 0m0.031s
Java version - java version "1.6.0_05"
______________________________________________________________________
$ time java tree
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at tree.create_tree(tree.java:13)
at tree.create_tree(tree.java:14)
at tree.create_tree(tree.java:14)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:14)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:14)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:14)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:14)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:15)
at tree.create_tree(tree.java:15)
at tree.main(tree.java:25)
real 0m3.367s
user 0m0.015s
sys 0m0.031s
Why is Java so crappy at memory management? I mean, I am able to make the
program work using C++ and a crappy little region allocator I whipped up. On
the other hand, Java simply runs out of memory and crashes! WTF is up with
that non-sense!?!?!?!?!
OUCH!
Am I doing something wrong in the Java version????
;^/
Well, I suppose I could create a cached allocator in Java, but won't that
completely defeat the purpose of not having to use manual memory management
in a garbage collected environment? I mean, you would have to explicitly
allocate/free objects from/to the cache. That's sounds manual to me!
;^)
BTW, the region allocator in the C++ code can be greatly improved upon. For
instance, why the heck am I using `std::list'?!?!? That's stupid because
it's going to be making extra allocations. I should use a simple intrusive
linked-list instead. But, you know, I wanted to give Java a fighting
chance...
;^o