Java faster than C++ ?? Impossible !

Discussion in 'C++' started by brey_maastricht, Jun 22, 2008.

  1. Dear all,

    I'm trying to rewrite a Java program into C++.
    The Java programm works fast but I hoped that C++ would even be
    But that is not the case !
    (to be complete: both the Java and C++ version of the program are
    intented to use within Matlab)

    I used as less 'new', 'delete' and 'delete[]' statements as possible.
    As less pointers as possible.
    I did use vectors, but no iterators. And I confirmed that there are no
    memory leaks at all.

    But still, the C++ version of my programm is slower than Java with a
    factor 100 !!
    I have used Visual C++ 6.0 and later on Visual Express 2008.

    How is that possible ? And are there people who had the same probem ??

    Kind regards,
    The Netherlands
    brey_maastricht, Jun 22, 2008
    1. Advertisements

  2. brey_maastricht

    Boris Guest

    Why shouldn't it be possible to develop a slow program in C++?

    Boris, Jun 22, 2008
    1. Advertisements

  3. brey_maastricht

    Mirco Wahab Guest

    If somebody says "Java is faster than c++", this
    usually implies that Java and C++ are not used as
    intended, with opposite signs. Some sophisticated
    Java-coding by some expert often beats a naive
    C++ implementation that can't avoid the pitfalls
    inevitably connected with such a complicated thing
    (like C++).
    Maybe the Java source was designed by a really clever
    programmer (who knows the VM's internals) and you
    tried to translate this beast too directly to
    C++? Who knows. What kind of code is it?


    Mirco Wahab, Jun 22, 2008
  4. brey_maastricht

    Ron AF Greve Guest

    Ron AF Greve, Jun 22, 2008
  5. ha scritto:
    A typical error is passing aggregate objects (in particular,
    collections) by value instead of by reference.
    It can have a huge impact.

    In this wikibook you can find many advices to improve the performance of
    your software:
    Carlo Milanesi, Jun 22, 2008
  6. That's what happens when you compile a debug version.
    Juha Nieminen, Jun 22, 2008
  7. brey_maastricht

    huili80 Guest

    The sentence "Java faster than C++" doesn't even make sense. How did
    one compare two languages? Did they mean it in a computational
    theoretical way that the intrinsics of C++ is doomed to be slower than
    that of Java, which has a mathematically sound proof? Or did they do a
    statistical research over a large number of C++ code and Java code
    that provide the same functionality?

    If you didn't provide any scientifically convincing comparation of C++
    and Java. You are probably better off saying something like this: "hey
    guys, I wrote a piece of C++ code that does the same thing as the Java
    code I wrote the other day. Guess what, my c++ program runs 100 times
    slower than my Java program. How surprising!!"

    To me, saying "Java faster than C++" based on someone's own very
    limited experience is nothing more than a joke. As far as I can see,
    it serves only to provide false hope for some and/or to intimidate
    huili80, Jun 22, 2008
  8. brey_maastricht

    peter koch Guest

    It is difficult to tell without you showing any code. But others have
    pointed out two likely sources: pass by value (which might look
    natural to a Java programmer), which can be a time-killer and running
    in debug mode, which easily can give give a factor of 10. I remember
    in my early days, where I accidentically passed a (large) structure by
    value. This one function caused made the program go five times slower.
    If there had been more functions like that, it is easy to imagine a
    factor of 100 as a result.
    That said, it very much depends on the program how much a speed-up,
    you could expect going to C++. I would not be surprised if you had no
    speed-up at all, and I would be surprised if you got a factor of much
    more than 2 unless the Java program should start trashing.

    peter koch, Jun 22, 2008
  9. brey_maastricht

    Daniel Pitts Guest

    In, we treat the converse claim as little more
    than trolling.

    The real problem is that Java and C++ run on different principals, so
    you can't truthfully say "I've written the exact same program in both

    Even Hello World is different:
    #include <iostream>
    int main(int argc, char **argv) {
    std::cout << "Hello World" << std::endl;
    return 0;
    class HelloWorld {
    public static void main (String[] args) {
    System.out.println("Hello World");

    Both of those programs have the same goal, but the process that happens
    is far different.

    For the C++ program, the C/C++ runtime is initialized, and it passes
    control off to the main method, which executes a function (operator<< )
    on a ostream object, and then executes another function (operator<< ) on
    the return value. What those operators do, I don't entirely know or
    care, as long as there effect matches my intention...

    For the Java program, the JVM is initialized (which probably includes a
    the C/C++ runtime being initialized), the system class loader loads the
    system classes, and then loads the HelloWorld class. After initializing
    the HelloWorld class, it looks for a static void main(String[] args)
    method, and invokes it. This method access the System class, reads a
    static field, and invokes a method on that object (which is of type

    So, the simplest programs do things very differently, even though they
    look similar enough. You can't compare Oranges to Orangutans.
    Daniel Pitts, Jun 22, 2008

  10. Hi,

    As I understand the problem, I could make a little guess about the

    1) MATLAB runs on Java platform. Therefore, native java programs might
    interact with MATLAB in a much efficient way than C++ programs. Don't
    know the internal mechanisms. Please be specific about waht do you
    intend to do with MATLAB.

    2) In java, you use pointers / referencing implicitely. But as you
    say, you avoid new, delete[] stuff in C++. This might be one reason
    for getting slower because of "passing by value's" in exchange of
    large data objects.

    3) Most part of the the java runtime is already loaded when MATLAB is
    running. But not the runtime requirements of the C++ program; May be,
    you have to load it for the first time and this might take time.

    All the best.

    II Ahmad Abdullah.
    iiahmadabdullah, Jun 23, 2008
  11. brey_maastricht

    W Karas Guest

    I think there is at least a hypothetical possibility that interpreted
    intermediate code could be faster than native code due to caching.
    Seems like it might be possible that the intermediate code together
    with the interpreter might fit in cache, whereas the corresponding
    native code would not. In this situation, if the native code was
    sufficiently lacking in locality, it might be slower than the
    interpreted code. But this seems unlikely, and even more
    unlikely to cause a slowdown by a factor of 100.
    W Karas, Jun 23, 2008

  12. Hi, One more point.

    The slow running of C++ might be due to its interaction with MATLAB.
    It can also be assumed that it's MATLAB that makes it delaying or
    delays in responding to your code and who knows how the interactions
    are and how it executes your C++ code, unless u clearly specify? GIve
    some more specific info about what u r trying to do with *MATLAB*.
    iiahmadabdullah, Jun 24, 2008
  13. brey_maastricht

    Lionel B Guest

    No, it can't be assumed. It should be tested for. My experience is that
    Matlab actually incurs a very small overhead in executing external
    (native) code. I suspect this is a red herring.
    <please don't shout; and drop the SMS-speak - thanks>

    AFAIK it is the Matlab *GUI* that is implemented in Java; the
    computational engine is almost certainly implemented in native code (e.g.
    Matlab will use native BLAS/LAPACK library code for many matrix
    Lionel B, Jun 24, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.