Re: Java outperforms C++?
- From: Jerry Coffin <jcoffin@xxxxxxxxx>
- Date: Sat, 16 Apr 2005 16:44:21 -0600
In article <Xns96399349F385Fqqasdfh76d3f4hasdf78@xxxxxxxxxxxxx>,
sdfjkhsdkh@xxxxxxxxxxxxxxxxxxxxxx says...
[ ... ]
> However I've found a caveat. Replacing
> bool state;
> with
> int state;
> and using
> state = -state
> instead of
> state = !state
> got results down to 3.9s. I didn't know that bool was so inefficient
> :)
....but that's not all. For only $49.95, the amazing C++ compiler not
slices and dices, but it can cook your coffee even faster than that!
What we have is still really transliterated Java. Creating the
objects with new and accessing them via pointers is just leftover
coffee grounds (so to speak). In real C++, they're just automatic
variables:
int
main(int argc, char *argv[]) {
int n = ((argc == 2) ? atoi(argv[1]) : 1);
bool val;
Toggle toggle(true);
for (int i=0; i<n; i++)
val = toggle.activate().value();
std::cout << std::boolalpha << val << std::endl;
NthToggle ntoggle(true, 3);
for (int i=0; i<n; i++)
val = ntoggle.activate().value();
std::cout << val << std::endl;
return 0;
}
This is taking 2.9 seconds for 1 000 000 000 iterations on a 2.8 GHz
P4. On the same machine, the Java version takes 21.8 seconds to run
the same number of iterations. IOW, where your code is about 5 1/2
times as fast as Java, this is more like 7 1/2 times as fast.
I'd also note that along with being a lot faster, getting rid of the
new and being able to write bools out directly makes this cleaner and
more readable as well.
Of course, the rest of the code is equally java-like. Just for
example, 'if (++this->counter >= this->count_max)' works just as well
as: 'if (++counter >= count_max)'.
C++ written by Java programmers reminds me of the old line about the
US and Great Britain being separated by a common language.
Then again, he's also made his Toggle::state public -- which I'd hope
even Java programmers would recognize is a truly bad idea. He's
apparently done this because his code in NthToggle manipulates state
directly instead of using the base class method to do so, as in:
Toggle& activate() {
if (++counter >= count_max) {
Toggle::activate();
counter = 0;
}
return(*this);
}
IMO, this is really the right way to do things, but even if he
insisted on manipulating the base class variable directly in the
derived class, he could have at least made the variable protected.
Then again, he does more or less the same thing in his Java as well
-- to its great detriment, as it turns out. In NthToggle::activate,
if we replace 'this.state = !this.state;' with 'super.activate();',
the Java version speeds up from 21.8 seconds to 8.5 seconds, or only
about 3 times slower than the C++ version.
Along with being a speedup for Java, this makes the code much more a
test of method calling -- clearly most the time in his Java code was
being spent on up-level variable access, not method calls.
Given that I'm not a Java programmer at all, and it was still easy
for me to speed his Java code up by a factor of about 2 1/2, I'm left
wondering whether competently written Java might actually be able to
keep up with C++. Unfortunately, if the benchmarks that purport to
compare the two are any indication, competently written Java is so
rare that we'll probably never know!
Then again, deriving 'NthToggle" from "Toggle" seems to indicate such
ignorance of the LSP that this person probably shouldn't be allowed
to use derivation again without completing a remedial OOD course. :-)
--
Later,
Jerry.
The universe is a figment of its own imagination.
.
- References:
- Java outperforms C++?
- From: benben
- Re: Java outperforms C++?
- From: $Scott
- Re: Java outperforms C++?
- From: Alex
- Re: Java outperforms C++?
- From: $Scott
- Re: Java outperforms C++?
- From: Alex
- Re: Java outperforms C++?
- From: Mark Randall
- Re: Java outperforms C++?
- From: Mark Randall
- Re: Java outperforms C++?
- From: Alex
- Java outperforms C++?
- Prev by Date: Simpel WIN32 Message box
- Next by Date: Re: Simpel WIN32 Message box
- Previous by thread: Re: Java outperforms C++?
- Next by thread: Re: Java outperforms C++?
- Index(es):
Relevant Pages
|