[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: gEDA-user: Languages etc
Under a previous thread......
On Tuesday 20 September 2005 01:06 am, Marvin Dickens wrote:
> On Tuesday 20 September 2005 12:00 am, Al Davis wrote:
> > On Monday 19 September 2005 11:22 pm, Marvin Dickens wrote:
> > > Regarding the speed of Java, it's as fast as C++ (Not C) if
> > > and only if it is run as a VM server ........
> >
> > Do you have a reference?
>
> Sure. Go to:
>
> http://kano.net/javabench/
There are some tests here that are seriously misleading.
Let me first pick the most flagrant: the "method call" test. It claims
Java is very much faster than C++... about a factor of 10 faster.
Looking closer, it is obvious why.
====== C++ code excerpt ============
class Toggle {
public:
Toggle(bool start_state) : state(start_state) { }
virtual ~Toggle() { }
bool value() {
return(state);
}
virtual Toggle& activate() {
state = !state;
return(*this);
}
bool state;
};
=========end C++ excerpt ============
=========Java excerpt ============
class Toggle {
boolean state = true;
public Toggle(boolean start_state) {
this.state = start_state;
}
public boolean value() {
return(this.state);
}
public Toggle activate() {
this.state = !this.state;
return(this);
}
}
===========end Java excerpt =========
The important word in the C++ program is "virtual". There are several
ways to make a method call. C++ lets you pick. By saying "virtual" it
forces the use of the slowest of all choices. In the Java code, he let
the compiler pick.
Here's another, the "object instantiation" test:
====== C++ code excerpt ============
Toggle *toggle1 = new Toggle(true);
for (int i=0; i<5; i++) {
cout << ((toggle1->activate().value()) ? "true" : "false") << endl;
}
delete toggle1;
for (int i=0; i<n; i++) {
Toggle *toggle = new Toggle(true);
delete toggle;
}
=========end C++ excerpt ============
=========Java excerpt ============
Toggle toggle1 = new Toggle(true);
for (int i=0; i<5; i++) {
System.out.println((toggle1.activate().value()) ? "true" :
"false");
}
for (int i=0; i<n; i++) {
Toggle toggle = new Toggle(true);
}
===========end Java excerpt =========
In Java, the objects are declared in the only way possible. In C++, it
is explicitly specified that they are in the persistent heap, with
explicit construction and destruction using new and delete. This is
the slowest way possible.
I changed it and got a 100:1 improvement. But Java was only 3x better
than C++. See how good C++ is now??? Or did the compiler optimize the
loop and its useless construction and destruction away, invalidating
the benchmark?
Now, putting aside the flagrant ones, he compares a particular C++
compiler to a paricular Java compiler. Maybe one compiler is better
than the other, and it has nothing to do with the language.
My point here is not to say "my language is better than your language",
or even to compare C++ to Java. Rather: don't trust the benchmark,
unless you really know what is happening.