Re: Java outperforms C++?



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.
.



Relevant Pages

  • Re: using matlab compiled dll without java support
    ... And i want to initalize the MCR without using java support, so I am doing the following: ... So it seems that java is loaded after all, and the plot window is sun window although I am initalizing with -nojvm. ... bool mclInitializeApplication; ...
    (comp.soft-sys.matlab)
  • Replace bool with int in a dll used in multi-languages
    ... I have a dll that is beeing called by C++, VBA, Java and C# ... One of my customers does not like that I have bool in the argument list ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Object serialization and NetworkStream - extraneous characters in output
    ... public static int Main ... bool done = false; ... TcpClient client = listener.AcceptTcpClient; ... returns output identical to the Java client. ...
    (microsoft.public.dotnet.framework)
  • Re: 1 or 1/0 doesnt raise an exception
    ... obtain complete evaluation in Java, you use & rather than && ... ... Although & IS the bitwise-AND operator in Java: ... bitwise for int and Boolean for bool, and in both cases they are strict. ...
    (comp.lang.python)
  • Re: OO refactoring trial ??
    ... As long as the concrete Python classes follow the form ... > in both Python and Java. ... > return values derive from a common base class, ... > pattern should return objects that share a common base class. ...
    (comp.lang.python)