Archived

This topic is now archived and is closed to further replies.

EbonySeraph

Speed of Different Programming Languages

Recommended Posts

I know this should go in another forum but I want as many replies as possible. Even if you see a reply that is just what you are going to say, please add what you may have more. This question is for a research project I am doing in school: What is the speed difference between C, C++, and Java? I mean in depth detail. I heard something in a post a while ago about C vs C++ and someone said something about C functions calls being faster. If anyone can explain why that would be greatly appreciated. Also if any one knows Java can someone tell me about why Java is slower than the other two languages. Also if you have any history(thought I have some) about the languages feel free to throw some in. Or if anyone knows a web site that has all the info or significantly chunked peices of it. Please tell me the URL. Thanks for your help. "Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Share this post


Link to post
Share on other sites
Java is slower than C, C++ because it compiles to javabyte. Javabyte is a kind of code which is interpreted by a Java Virtual Machine (A piece of software which MUST be installed on a computer to execute java programs) which thens turn it into machine code for the processor. This allows a java program to run on any system (which has a JVM), but means the code must first be interpreted then executed, instead of just being executed straight away like in C, C++.

Java was originally created to be used in electronic devices (eg. home appliances such as VCR''s). It was unsuccessful, so the creator''s decided to modfiy it to allow it to be used with the Internet (ie. Applet''s), which created an obvious step to allow it to build applications.

Hope that helps, on java at least. I''m studying/programming Java at university, so that''s why I know a bit about it.

MelvinElvin

Share this post


Link to post
Share on other sites
Yup, Java was developed by Sun Microsystems in the early 90''s. It''s original objective was to create a system to comunicate to different house-hold appliances. When that failed it was used as a completely system independent programming language ( the reason you need a JVM ).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
there''s some real good information at the begining of this one book, core java fundamentals. i think it''s written by sun. it has some good information on the background of java and how it''s compiled and why it''s slower. just read the first chapter in a bookstore or something

Share this post


Link to post
Share on other sites
as far as i know C is faster than C++ one reason is because it''s standardized/optimized universally already, and C++ still has some features that have yet to be fully optimized among different compilers, such as the string class and things like that. i''m not too sure this is a reason, but i know i heard it somewhere, so it should hold some meaning.

also, this should also be verified among other programmers, since C++ is an object-oriented language as opposed to C''s procedural (step by step) language, C++ must go through extra steps to accessing the right data members and methods of all the classes in order to execute.

and regarding java, the others above are correct.

a2k

Share this post


Link to post
Share on other sites
There's no significant speed difference between C and C++ anymore. C++ used to be poorly compiled due to being converted to C and then compiled with standard C compilers. Modern optimizing C++ compilers do not do this and do a really good job at optimizing C++.

Of course, C++ in practice can be a bit less efficient than C. This is not due to any problem with C++, its due to abstraction allowed by object oriented design. When setting a variable in a C++ class, for example, you might call methods like SetVariable(), whereas in C you would just set the variable directly in your main code flow. This is a conscious design decision on the C++ programmer's part to abstract the underlying data from the class interface, and it is usually worth it because it results in code that is easier to maintain and understand in large projects.
A good C++ programmer will determine when these little bits of overhead are worth it and when they are not, and will use tools such as inline functions to construct code that is essentially just as fast as the same algorithm in C.

C++ function calls aren't slower by default than C function calls (something the original poster asked about). There is an exception to this: C++ virtual functions (used to allow sane polymorphism in base classes) are ever so slightly less efficient during the function call setup because it has to call into the function through a table, its not a direct function binding. Again, a good C++ programmer will be able to decide when virtual functions are worth it for the ease of clean design they offer and when they are not (tight loops, etc).

As far as Java goes, Java can be quite fast, as others have mentioned. You can actually compile Java direct to machine code using some tools such as GCJ. In doing so you often lose some of the nice features of Java, such as reflection, but as always a good programmer has to weigh the pros and cons of each design and implementation detail: is it worth it? It depends.

There are other Java technologies like Hotspot that allow you to use the full Java language and optimize code at runtime. As others have mentioned these can make the code very fast, but these are generally more useful for long running (usually server side) programs. A client program needs to be responsive right away, not 5 hours after it starts running. This has really been the sticking point of Java. Despite Sun's past claims of C++-like execution, a GUI-based Java program using Swing is generally noticably slower than a similar C++ program on the same hardware. In its defense, Sun has been doing a lot to address the speed issues, and there are some features in Java 1.4 that should help out even more, but as of right now they still exist and can be a problem depending upon the size and complexity of the Java program you want to distribute.

Another potential drawback of Java is that deploying Java applications can be more complex than usual as you need to make sure the user has an appropriate JVM installed on his/her system. You can include a JVM installer within your app's installer but that will generally balloon the size of your application by several megabytes. Not a problem for CD distribution, but can be a problem for net distribution.


Edited by - gmcbay on August 3, 2001 2:09:40 PM

Share this post


Link to post
Share on other sites
Any C++ method will be slightly slower than a C function, because there is an extra stack push to pass what instance of the object you''re calling from. This is basically negligible with today''s PCs, but I remember people were concerned about it when C++ was first introduced (myself included. Heh.) Of course, at the time people would nitpick just about anything about object oriented programming. Structured coding (killing the goto) had the same initial negative reaction.

Java''s slowness is often attributed to bytecode, but that isn''t really true. You can get java compilers that compile to native code, and it still runs slower than C++. This is because Java includes features that C++ does not, such as run time array bounds checking and garbage collection. More features takes more time.

Share this post


Link to post
Share on other sites
quote:
Original post by CheeseGrater
Any C++ method will be slightly slower than a C function, because there is an extra stack push to pass what instance of the object you''re calling from.

No! This is a common misconception. Firstly, being pedantic, the ''extra'' parameter rarely goes on the stack. For example, in Visual C++, I believe it is passed in the EBX register. Secondly, and mainly, all you''re doing is passing a pointer to the class. To do the same thing in C, you would have to pass a pointer to the struct anyway.
C++ call: MyObject->SomeFunction(Param1, Param2);
C call: SomeFunction(MyObject, Param1, Param2);

The only difference is that for the C++ version, one of the parameters (the pointer to MyObject) is ''hidden'' because it''s not an explicit parameter. This is an example of how C++ has ''hidden'' overhead... however, in total, it still adds up to exactly the same amount of data being passed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I recognize that passing in a structure causes an equal performance hit. When we used to voice that concern, what we were really complaining about was that encapsulation would cause additional overhead _when you only had one instance_.

When there''s one instance, there''s no extra information to pass, so you can globalize it and remove what was at the time a stack push/pop, and is now just a register move.

Gosh, I haven''t even thought of this junk in years. We cared about this when we were programming for Xwindows on 20Mhz MIPS processors (Heh. My first Xwindows 3D rendering program ran at 0.1 FPS). The function overhead basically makes no difference on today''s monster P4''s. But hey, thy guy wanted history. Maybe the fact that programmers initially resisted C++ for reasons like that will make a nice footnote.

Share this post


Link to post
Share on other sites
quote:
Original post by CheeseGrater, masquerading as an Anonymous Poster
I recognize that passing in a structure causes an equal performance hit. When we used to voice that concern, what we were really complaining about was that encapsulation would cause additional overhead _when you only had one instance_.

When there''s one instance, there''s no extra information to pass, so you can globalize it and remove what was at the time a stack push/pop, and is now just a register move.

This is not a difference between the languages. This is a difference between object-oriented or well-structured code against global variables. You can do exactly the same in C++ if you want globals hanging around your code. In that case, you would use a static member function in C++, which is exactly equivalent to a function in C, which means they have equivalent execution time. On the other hand, if you don''t want globals, you would have to use one of the function syntaxes I showed above, which again, have equivalent execution time. The fact remains: to achieve the same task takes the same length of time in C++ as it does in C. There is no overhead.

PS. Dactylos is right: it''s actually the ECX register for the ''this'' pointer.

Share this post


Link to post
Share on other sites
If there''s only one instance, the class can have all static methods and static properties, which means no this pointer is passed in ecx, saving you that extra mov, and should compile to identical asm as the C procedures operating on global variables. As mentioned, this all died with the 386 if not sooner.

I''ve yet to use a Java program that has acceptable performance. Imagine if Excel or Word was written in Java... it''d take 10 minutes to load, and 2 hours to recalculate. I know there are claims out there that Java programs can run just as fast as C++ programs, and even ''performance data'' that supports this. But I''ve never used a Java program that didn''t seem sluggish. The performance varies wildly depending on the JVM that''s used.

...
quote:

I can''t imagine anyone changing their class hierarchy to optimize one loop...


They''re usually abandoned if the design was that bad.

Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites
Just out of interest I think that Sun made a processor (called Pico?) that runs Java bytecode natively. In my opinion C++ is no slower that C, and if it is it is because the programmer wrote it slow. Virtual functions are about the only thing the slows C++ down (and it''s negliable). One could argue that C++''s inline methods speed it up compared to C (I don''t cause this is a programmer decision, but some might). In the old days it was slower, now it''s pretty good. Oh! Actually, if you use extensive exception throwing it will be a bit slower.

Brad

Share this post


Link to post
Share on other sites
You might wanna check out:
http://www.ubka.uni-karlsruhe.de/cgi-bin/psview?document=ira/2000/5&format=1&page=13

You also might want to include Python in your research paper

"Journies Lead to Knowledge and Passion Lights the Way..."

~=NeuroMorphus=~

Share this post


Link to post
Share on other sites