aspose file tools*
The moose likes Performance and the fly likes Which is faster : C or Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Which is faster : C or Java" Watch "Which is faster : C or Java" New topic
Author

Which is faster : C or Java

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
According to me C should be fast,but through results on google i am not clear which one of two is faster and why??Also tell about overall performance of a C program and a java program.kindly let me know..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Java unfortunately still has a bad reputation with regard to speed, which is completely undeserved.

Ten years ago, when we didn't have a sophisticated JIT (Just-In-Time) compiler in the JVM and (almost) all bytecode had to be interpreted while the program ran, yes, Java was slow. I'm talking about Java version 1.0 and 1.1 here.

Today, Java is not slow at all. Recently I picked up some C++ again (after having been programming almost exclusively in Java for the last 5 or 6 years). I wrote a small ray tracing program, which is very computation-intensive, in Java and in C++. To get the C++ program to run as fast as the Java program, I had to carefully set the optimization options on the C++ compiler.

So if someone tells you that Java is slow (slower than C or C++), ask him or her to prove it to you. Most likely the person is just repeating what he or she heard from others, without any proof.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Prashant kumar Singh
Greenhorn

Joined: Nov 04, 2006
Posts: 22
Hi,
You can easily understand that which one is fast.First C is Hardware friendly language.and java is User friendly.And java uses an interface in between user and Machine is Bytecode.But in begining age of java, it was very slow.But in this age, means java5, java is better in speed.it also depends upon arctitecture of Virtual machine.Some jvm works very fast like Jrockit.

Some people may say that C is more friendly to hardware than java. So C is fast. But it was true in past .But at this time Java is betterthats why all big projects are based on java concept.
Thats all

thanks
Prashant singh
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Moving to our performance forum...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The question really can't be answered. Both C and Java are compiled to machine code (just at different times - C at compile time, Java at runtime). Which program will be faster will depend on what machine code is produced - which depends on both

- what the source code looks like, and
- what program (C-Compiler/Java Virtual Machine) you use to generate machine code from it

Actually it could be argued that conceptually the JVM has two advantages:

- it can perform additional runtime analysis of the program to further optimize it, and
- it knows *exactly* what platforms it runs on, being able to perform special optimizations for that platform


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Performance issues between C applications and Java applications have more to do with the environment than the program itself. You can get both of them to do pretty much the same thing, in the same amount of time. At least that has been my experience.

Furthermore, Java often runs on the server-side. People complaining about Java being slow are likely complaining about the slowness of an internet connection, or poor use of connectoin pooling by an application server, none of which has any relationship to the execution of a Java program itself.

-Cameron McKenzie
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Have a look at this blog entry:

http://scienceblogs.com/goodmath/2006/11/the_c_is_efficient_language_fa.php

Basically the writer says that for applications where it matters (to him), Java and C both run equally as fast but other languages are faster.

So if you're looking for a single answer to your question, then this reference says that they're both equally fast. Of course it is pointless to provide a single answer since it really depends on what you want these Java or C programs to do.
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
aperf1

aperf2

Does this make sense?

G.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Guy Allard:
aperf1

aperf2

Does this make sense?

G.


Not to me, but I may just be dumb. Care to elaborate?
Neerav Narielwala
Ranch Hand

Joined: Dec 08, 2006
Posts: 106
Please see the below link which tells us C vs Java detais :
http://www.cs.princeton.edu/introcs/faq/c2java.html


<a href="http://www.java-tips.org/java-tutorials/tutorials/" target="_blank" rel="nofollow">Java Tutorials</a> | <a href="http://www.planet-java.org" target="_blank" rel="nofollow">Java Weblog</a> | <a href="http://computer-engineering.science-tips.org" target="_blank" rel="nofollow">Computing Articles</a>
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Neerav Narielwala:
Please see the below link which tells us C vs Java detais :
http://www.cs.princeton.edu/introcs/faq/c2java.html


Not a word about performance, though...
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

thanks to Paul for his link (scienceblogs).

The huge difference between older and new Java compilers pointed there was really surprising.

And you made me think about FORTRAN again...

Bu.


all events occur in real time
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Which is faster?

Like, if I needed to hire 10 programmers, would it be faster to find 10 young programmers that know C++, or 10 young programmers that know Java?

Or if I had a problem, which would I likely get faster support?

Or which one, C++ or Java, makes it faster to create a transactional web based application, such as J2EE or CORBA?

Hmmmmm. Which one is faster? I think Java.

And the JIT compiler means Java is no slouch at runtime, either.

-Cameron McKenzie
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Cameron W. McKenzie:
Which is faster?

Like, if I needed to hire 10 programmers, would it be faster to find 10 young programmers that know C++, or 10 young programmers that know Java?

Or if I had a problem, which would I likely get faster support?

Or which one, C++ or Java, makes it faster to create a transactional web based application, such as J2EE or CORBA?

Hmmmmm. Which one is faster? I think Java.

And the JIT compiler means Java is no slouch at runtime, either.

-Cameron McKenzie


Initially java was much slower , but now it has improved a lot.Though I have not done any sort of benchmarking as I do not know much of C/C++ , but I still think that java might be bit slower than C / C++.Though java has JIT but it only compiles qualifying code to native not all of the bytecode.


Rahul Bhattacharjee
LinkedIn - Blog
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Originally posted by Ilja Preuss:


Not a word about performance, though...


Yea, Neerav seems to have a history of posting to be posting.


GenRocket - A Test Data Generation Platform
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Originally posted by Rahul Bhattacharjee:


Initially java was much slower , but now it has improved a lot.Though I have not done any sort of benchmarking as I do not know much of C/C++ , but I still think that java might be bit slower than C / C++.Though java has JIT but it only compiles qualifying code to native not all of the bytecode.


Wow, repeating what has already been said in this thread.

Seems to me that worrying about which is faster is a moot point. That's right, I used the word moot. Even if Java is slower at executing code than C/C++, I'd still choose Java for 98% of what I would need to do because most of the time, the execution of code is not the bottleneck in an application. Not to mention the speed of development time in Java compared to C/C++, which was already stated.
Martin Podrouzek
Greenhorn

Joined: Sep 09, 2004
Posts: 10
Fine day to you,

recently I attended a conference about Java in Antwerps. There was a notable speech of Brian Goetz about "Performance Myths in Java".

Here I link some articles of the Brian:
http://www-128.ibm.com/developerworks/views/java/libraryview.jsp?search_by=practice:

Just search for word "performance". I also recommend testing the code of Brian's. Have fun

Martin
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

The question is moot. They are not comparable as such. This is not like asking which is faster C or Assembly. Both of those run directly on the CPU.

Java code on the other hand does not run directly on the CPU. It runs in a Java Virtual Machine. That JVM is typically written in C/C++.

The question then becomes, which 'c' program is faster. Your code, or the Sun/IBM JVM?
Pj Murray
Ranch Hand

Joined: Sep 24, 2004
Posts: 194
Java has caught up with C++ in almost every runtime performance in almost every respect - except memory allocation.

So C++ is still a better choice if performance is a concern and there's large amounts of data to be handled.

Otherwise, Java is faster and therefore cheaper to develop with.


PJ Murray -
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 396

"Java has caught up with C++ in almost every runtime performance in almost every respect - except memory allocation.

So C++ is still a better choice if performance is a concern and there's large amounts of data to be handled."


Personally I thought Java memory allocation is better ...

http://www-128.ibm.com/developerworks/java/library/j-jtp09275.html

Basically my favourite story on this argument was an article in the C++ users group where they gave the same code to a java guru and C++ guru the Java implenation on a modern JVM finally out sped the C++ version. However the C++ guy went away twiddled the code and compiler settings and managed to surpass the Java with a days work, but it took a lot of work.

Having done a lot of both I'ed recommend java in most cases though there are still areas where C++ is the tool of choice just a lot less of them now, its no longer my generic language of choice .


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Pj Murray
Ranch Hand

Joined: Sep 24, 2004
Posts: 194
Originally posted by Chris Hurst:
[i]

Having done a lot of both I'ed recommend java in most cases though there are still areas where C++ is the tool of choice just a lot less of them now, its no longer my generic language of choice .


Yes - I think you've nailed it.
ws hong
Greenhorn

Joined: Feb 15, 2007
Posts: 3
I thought c was fast, but now after I read the post,the idea changed,Thanks you above!
[ February 15, 2007: Message edited by: wshong wshong ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

"wshong wshong",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 225
Please read out the following faq of Performance issue between java and C using some standard BenchMarsk.

http://www.idiom.com/~zilla/Computer/javaCbenchmark.html

I hope this will answer the questions.


The difference between <b>failure</b> and <b>success</b> is often being <b>right</b> and being <b>exactly right</b>.
Pj Murray
Ranch Hand

Joined: Sep 24, 2004
Posts: 194
Originally posted by Gregg Bolinger:


Wow, repeating what has already been said in this thread.



In fact, this whole thread is something of a time warp back several years.

I'm actually old enough to remember that before the Java versus C++ debate, there was a debate between the C and assembly language.

I wonder if a new major general programming language will emerge to we can repeat the cycle but this time having Java as the legacy language?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

Originally posted by PJ Murray:
In fact, this whole thread is something of a time warp back several years.

I'm actually old enough to remember that before the Java versus C++ debate, there was a debate between the C and assembly language.
Even I am not old enough to remember that back in the dawn of history there was that same debate between assembly language and the basic machine instructions, as you can read here:

http://scienceblogs.com/goodmath/2007/03/sad_news_john_backus_has_died.php
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which is faster : C or Java
 
Similar Threads
Cursor
Speed in Java
Who's using threads?
Calling C or C++ methods from Java
What to learn first for Java?