Re: Do you use a garbage collector (java vs c++ difference in "new")
On Apr 11, 3:37 am, Razii <DONTwhatever...@hotmail.com> wrote:
On Fri, 11 Apr 2008 03:35:27 +0300, Juha Nieminen
<nos...@thanks.invalid> wrote:
Razii wrote:
In C++, each "new" allocation request
will be sent to the operating system, which is slow.
That's blatantly false.
Well, my friend, I have proven you wrong. Razi has been victorious
once again :)
Time: 2125 ms (C++)
Time: 328 ms (java)
What do you think you have proved? I see that MSC allocator is slower
than Java's, all right. But you have not proved that it is calling
system directly.
Besides, the code is not equivalent.
--- c++--
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
class Test {
public:
Test (int c) {count = c;}
virtual ~Test() { }
int count;
};
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;
// you need to add here
delete test;
// because Java does not hold reference to created pointer, so it gets
GCed soon
}
clock_t endt=clock();
std::cout <<"Time: " <<
double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
}
-- java ---
import java.util.*;
class Test {
Test (int c) {count = c;}
int count;
public static void main(String[] arg) {
long start = System.currentTimeMillis();
for (int i=0; i<=10000000; i++) {
Test test = new Test(i);
if (i % 5000000 == 0)
System.out.println (test);
}
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - start) + " ms");
}
}
If you have time, fix and try with U++.... (it has overloaded new/
delete).
(Note: I do not know what the result will be, I guess it will still be
slower than Java, I am just interested :)
Mirek