Java faster than C++ ?? Impossible !

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

  1. Guest

    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
    faster.
    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,
    Brey
    The Netherlands
     
    , Jun 22, 2008
    #1
    1. Advertising

  2. Boris Guest

    On Sun, 22 Jun 2008 14:09:13 +0200, <> wrote:

    > I'm trying to rewrite a Java program into C++.
    > The Java programm works fast but I hoped that C++ would even be
    > faster.
    > But that is not the case !
    > [...]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 ??


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

    Boris
     
    Boris, Jun 22, 2008
    #2
    1. Advertising

  3. Mirco Wahab Guest

    wrote:
    > 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 !!


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

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


    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?

    Regards

    M.
     
    Mirco Wahab, Jun 22, 2008
    #3
  4. Ron AF Greve Guest

    Hi,

    I assume you did a release build in VC++ to compare it with java? A debug
    build is indeed quite slow

    Regards, Ron AF Greve

    http://www.InformationSuperHighway.eu

    <> wrote in message
    news:...
    > 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
    > faster.
    > 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,
    > Brey
    > The Netherlands
     
    Ron AF Greve, Jun 22, 2008
    #4
  5. ha scritto:
    > I'm trying to rewrite a Java program into C++.
    > The Java programm works fast but I hoped that C++ would even be
    > faster.
    > But that is not the case !
    > How is that possible ? And are there people who had the same probem ??


    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: http://en.wikibooks.org/wiki/Optimizing_C++

    --
    Carlo Milanesi
    http://digilander.libero.it/carlmila
     
    Carlo Milanesi, Jun 22, 2008
    #5
  6. wrote:
    > But still, the C++ version of my programm is slower than Java with a
    > factor 100 !!


    That's what happens when you compile a debug version.
     
    Juha Nieminen, Jun 22, 2008
    #6
  7. Guest

    On Jun 22, 8:09 am, wrote:
    > 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
    > faster.
    > 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,
    > Brey
    > The Netherlands


    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
    others.
     
    , Jun 22, 2008
    #7
  8. peter koch Guest

    On 22 Jun., 14:09, wrote:
    > 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
    > faster.
    > 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 ??


    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
     
    peter koch, Jun 22, 2008
    #8
  9. Daniel Pitts Guest

    wrote:
    > On Jun 22, 8:09 am, wrote:
    >> 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
    >> faster.
    >> 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,
    >> Brey
    >> The Netherlands

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

    In comp.lang.java.programmer, 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
    languages."

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

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

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jun 22, 2008
    #9
  10. Guest

    On Jun 22, 10:01 pm, Daniel Pitts
    <> wrote:
    > wrote:
    > > On Jun 22, 8:09 am, wrote:
    > >> 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
    > >> faster.
    > >> 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,
    > >> Brey
    > >> The Netherlands



    Hi,

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

    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.
     
    , Jun 23, 2008
    #10
  11. W Karas Guest

    On Jun 22, 8:09 am, wrote:
    > 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
    > faster.
    > 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,
    > Brey
    > The Netherlands


    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
    #11
  12. Guest

    On Jun 22, 5:09 pm, wrote:
    > 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
    > faster.
    > 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,
    > Brey
    > The Netherlands



    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*.
    NOTE: MATLAB IS A JAVA PROGRAM. NOT A NATIVE WIN32 OR ANY OF THAT SORT.
     
    , Jun 24, 2008
    #12
  13. Lionel B Guest

    On Mon, 23 Jun 2008 23:55:38 -0700, iiahmadabdullah wrote:

    > On Jun 22, 5:09 pm, wrote:
    >> 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 faster.
    >> But that is not the case !
    >> (to be complete: both the Java and C++ version of the program are
    >> intented to use within Matlab)


    [...]

    > 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


    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.

    > 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*. NOTE: MATLAB IS
    > A JAVA PROGRAM. NOT A NATIVE WIN32 OR ANY OF THAT SORT.


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

    --
    Lionel B
     
    Lionel B, Jun 24, 2008
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    43
    Views:
    7,499
    Benji
    Nov 2, 2005
  2. Julie
    Replies:
    140
    Views:
    5,142
    George Neuner
    Jun 6, 2004
  3. Sanny
    Replies:
    12
    Views:
    599
    Andrew Thompson
    Dec 15, 2006
  4. Replies:
    5
    Views:
    294
    Michele Dondi
    Jun 30, 2006
  5. kramer65
    Replies:
    25
    Views:
    375
    Steven D'Aprano
    Mar 5, 2013
Loading...

Share This Page