Thread: why is C faster than C++ and java?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    37

    why is C faster than C++ and java?

    any one please explain it

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is a topic of much debate, and you should do your research before asking the question. For example, a quick search brings up this debate: C faster than C++ Myth.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    37
    how could you think that I wouldn't have searched before asking for.? don't explain how to ask smart questions. I've went through the link you have posted already and whatever it says I know.
    actually I hoped to get a better reason.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is it?

    I think you have to define under what circumstances - as different languages [using their natural features to solve problems] often lend themselves do different solutions with more or less efficient.

    In general, nearly all code written in C++ should be as efficient as code written in C as long as:
    1. The goal of the code was to write efficient code (rather than efficient use of the programmers time).
    2. The programmer understands what he/she is doing.

    Naturally, if we use STL to "help" in speeding up the development, it is possible that it's not quite as fast as the same code written in C by someone with sufficient skill - but it's often not STL's fault, but rather the programmer not understanding how to make STL get the right performance. Recently I wrote a little benchmark comparing C style array and C++ vector. Whilst my results where slightly in favour of array, it was small enough that most people would agree that "it doesn't matter much". Others then tried to replicate my results, and found the STL implementation was actually FASTER - I suspect that some other factors where involved here, but the key being that there's little difference in most things.

    Comparing Java in non-native compiled mode with a native compiled environment is like comparing Apples with Bananas - they don't grow in the same climate, they have completely different shape, and taste quite different too. Java tries to solve the problem of needing to compile code for every machine it runs on by having a Java VM in between the actual hardware and the Java code itself - this does consume some extra overhead. Java in Native Compiled mode is quite capable of competing within the same range as C++ - it may not beat C++, but it's not 10x slower - it is 10-20% slower, which for MOST things don't matter, and if it's something that is REALLY critical, it is possible [at the further loss of portability of course] to link Java code with C or C++ code. Of course, it also depends on which compiler we use for each side of the equation.

    Some references:
    http://www.idiom.com/~zilla/Computer...benchmark.html
    http://www.bioinformatics.org/benchmark/results.html
    http://www.freewebs.com/godaves/javabench_revisited/
    http://shootout.alioth.debian.org/

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    how could you think that I wouldn't have searched before asking for.?
    You did not tell us anything about what you already knew of the matter.

    I've went through the link you have posted already and whatever it says I know.
    So what do you know?

    actually I hoped to get a better reason.
    How do you know that "C (is) faster than C++ and java"? What do you mean when you make such a comparison?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by san_crazy View Post
    any one please explain it
    Why is green more visually appealing than blue?

    See what I did there was make up an absolute yet arbitrary answer in the form of a question, in an attempt to start a flame war.

    You think you have a firm grasp of 'how to ask questions the smart way' but you've obviously only skimmed the link in question and haven't really taken any of it in.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    C++ should only be slower when you use C++-specific features. Once I tested an MD5 algorithm in C and one in C++. The speed difference was about 5-6x (!!!). The difference would've been very small or nonexistent when the C code would've been compiled as C++ and then tested.

    Java is mostly not run as a machine code application. Instead, Java Runtime Environment (JRE) reads and interprets applications compiled with Java. That is the reason why it is slower.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    However in some cases Java can be faster, especially with just-in-time compiling.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    don't explain how to ask smart questions.
    No need to get offended; thats laserlight's signature

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Because of Dennis Ritchie. Duh.

    He may be a little slower now, but he's one of those quiet types that you have to watch out for. They come out of nowhere...

    He's got enough juice to handle Stroustrup and Gosling with some left over.

    Ritchie has already solved all of the known problems, and most of the unknown ones. C programming is just unlocking those answers, and that's why it is so much faster than C-- or java. Seriously.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by maxorator View Post
    C++ should only be slower when you use C++-specific features. Once I tested an MD5 algorithm in C and one in C++. The speed difference was about 5-6x (!!!). The difference would've been very small or nonexistent when the C code would've been compiled as C++ and then tested.
    I find this hard to believe. Either you were comparing different compilers, or something was botched with the comparison.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sander View Post
    I find this hard to believe. Either you were comparing different compilers, or something was botched with the comparison.
    Or simply that one implementation was good and the other not so good? No matter what language you use, you can make good and bad implementations - typically, in C++, it would be pessimisations like passing a vector without const reference when const reference can be used - if you pass around the entire block of data being processed a few times, it can easily take 5x longer than the same code in C [that passed an array -> pointer to the data].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well, I went to the MD5 "unofficial homepage", where there were implementations for many languages. I took the ONLY C++ implementation, and chose one of three possible C implmentations. The result was that in C++ the amount of MD5s calculated in one second was around 100 000 - 120 000. In C it was around 600 000. And I found a MASM implementation too, which resulted around 2 000 000 per second.

    -------------------------------------------------------------------------------------------------------------------------------------
    Edit: My result checking system I used in C and C++ may have affected the results on the previous very old tests due to its ineffectivity. Now I removed result checking and made C and C++ use the same codebase for using the test.
    Code:
    1 900 000 - 1 950 000 --- MASM implementation with result checking and ASCII generating
    800 000 - 870 000 --- C implementation with ASCII generating, without result checking
    270 000 - 330 000 --- C++ implementation with ASCII generating, without result checking
    ASCII generating means generating the strings for brute-force test like "a", "b", ... , "ab", ... , "ba" and so on.
    No comments.
    Last edited by maxorator; 08-19-2008 at 04:09 AM.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I made some tests using the time_trial version in md5-cc "driver.c" and the "L. Peter Deutsch" code.

    The C++ code is very old and needed a bit of massaging to get to compile with g++ 3.4.2

    I do not get as much difference as maxorator - which may be due to gcc being less optimal, the test-case being different, different processor, or some other variation.

    I use -O3 -fomit-frame-pointer (I tested a few other settings too, and this is so far the best one).
    Blocks used for testing:
    #define TEST_BLOCK_LEN 1000
    #define TEST_BLOCK_COUNT 1000000

    md5-cc: 5.578 seconds, 179275726 bytes/second
    md5-c: 4.124 seconds, 242483026 bytes/second

    That makes the C implementation 35% or so faster than the C++ one.
    Maxorators results are about 3x difference between C++ and C.

    Maxorator: Did you replace the md5::memcpy() and md5::memset() with the standard library variants - that makes a bit of a difference in my tests.

    And in the defence of the C++ solution, it makes much less use of macros to provide the calculations. I expect that some work with macros and/or templates could improve the C++ version to equal the C version. Some proper profiling may also help a bit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed