423,323 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,323 IT Pros & Developers. It's quick & easy.

How much fast is C++ than Java?

P: n/a
I have an app in Java. It works fine. Some people say Java works as
fast as C. Is that true?

C can use assembly language programs. How much faster are they inplace
of calling general routines.

Can C++ directly acess the Registers.

Will the Computation 5-10 times faster than Java?

Is there any benchmark which tell us these things.

Bye
Sanny

C++ Experts Needed: http://www.getclub.com/Experts.php
Jun 27 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Sanny" <so*******@hotmail.comwrote in message
news:9c**********************************@y18g2000 pre.googlegroups.com...
>I have an app in Java. It works fine. Some people say Java works as
fast as C. Is that true?
No. C will typically be about 5-10 times faster than the same application
written in Java, if the bottleneck is in code written in those languages.
That's an important point, because often the bottleneck is the rasteriser on
the grapj=hics card or something similar, in which case C will have no
advantage.

However it rarely matters on modern PCs. The screen now usually updates
within a frame, so for any interactive work either Java or C is acceptable.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #2

P: n/a
bl****@myrealbox.com <bl****@myrealbox.comwrites:
In article <1p*********************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>>
"Sanny" <so*******@hotmail.comwrote in message
news:9c**********************************@y18g200 0pre.googlegroups.com...
>I have an app in Java. It works fine. Some people say Java works as
fast as C. Is that true?
No. C will typically be about 5-10 times faster than the same application
written in Java, if the bottleneck is in code written in those languages.
That's an important point, because often the bottleneck is the rasteriser on
the grapj=hics card or something similar, in which case C will have no
advantage.

In my (admittedly limited, but more than zero) experience, C
is indeed faster, but not by anything like a factor of 5 to 10.
I'm curious to know what kinds of applications, on what systems,
exhibit such dramatic differences. I just did a quick check
using a toy numerical-integration example for a class I teach,
and the Java version took about 2.9 seconds, while the C version
took about 1.6 seconds. This is on a reasonably fast Intel-based
It all depends on the applications and on how much time is spent in the
library functions. It really doesn't take a rocket scientist to realise
that C, written by a competent programmer, will generally be faster than
Java.

Jun 27 '08 #3

P: n/a
In article <ft**********@registered.motzarella.org>,
Richard <de***@gmail.comwrote:
bl****@myrealbox.com <bl****@myrealbox.comwrites:
In article <1p*********************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>
"Sanny" <so*******@hotmail.comwrote in message
news:9c**********************************@y18g2000 pre.googlegroups.com...
I have an app in Java. It works fine. Some people say Java works as
fast as C. Is that true?

No. C will typically be about 5-10 times faster than the same application
written in Java, if the bottleneck is in code written in those languages.
That's an important point, because often the bottleneck is the rasteriser on
the grapj=hics card or something similar, in which case C will have no
advantage.
In my (admittedly limited, but more than zero) experience, C
is indeed faster, but not by anything like a factor of 5 to 10.
I'm curious to know what kinds of applications, on what systems,
exhibit such dramatic differences. I just did a quick check
using a toy numerical-integration example for a class I teach,
and the Java version took about 2.9 seconds, while the C version
took about 1.6 seconds. This is on a reasonably fast Intel-based

It all depends on the applications and on how much time is spent in the
library functions. It really doesn't take a rocket scientist to realise
that C, written by a competent programmer, will generally be faster than
Java.
Probably so (despite the occasional claims by Java advocates
about the magic that can be worked via JIT compiling). But -- a
factor of 5 or 10? That's entirely plausible if the Java runtime
system is strictly a byte-code interpreter, as I understand the
early implementations were, but these days most of them do that
JIT compiling to native code, and while compiling isn't free,
it seems to me that if the cost of compiling a class is amortized
over many calls to its methods ....

Well. I don't mean to incite a flame war here -- I use and like
both Java and C -- but .... I wouldn't disagree with "Java is
generally slower, sometimes by a lot", but "Java is generally 5
to 10 times slower" seems to me to be too strong a claim. But I
admit that my perception is based more on anecdote and hearsay
than careful research.

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Jun 27 '08 #4

P: n/a
bl****@myrealbox.com wrote:
In article <ft**********@registered.motzarella.org>,
Richard <de***@gmail.comwrote:
>bl****@myrealbox.com <bl****@myrealbox.comwrites:
In article <1p*********************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:

"Sanny" <so*******@hotmail.comwrote in message
news:9c**********************************@y18g2000 pre.googlegroups.com...
>I have an app in Java. It works fine. Some people say Java works
as
fast as C. Is that true?

No. C will typically be about 5-10 times faster than the same
application written in Java, if the bottleneck is in code written
in those languages. That's an important point, because often the
bottleneck is the rasteriser on the grapj=hics card or something
similar, in which case C will have no advantage.

In my (admittedly limited, but more than zero) experience, C
is indeed faster, but not by anything like a factor of 5 to 10.
I'm curious to know what kinds of applications, on what systems,
exhibit such dramatic differences. I just did a quick check
using a toy numerical-integration example for a class I teach,
and the Java version took about 2.9 seconds, while the C version
took about 1.6 seconds. This is on a reasonably fast Intel-based

It all depends on the applications and on how much time is spent in
the library functions. It really doesn't take a rocket scientist to
realise that C, written by a competent programmer, will generally be
faster than Java.

Probably so (despite the occasional claims by Java advocates
about the magic that can be worked via JIT compiling). But -- a
factor of 5 or 10? That's entirely plausible if the Java runtime
system is strictly a byte-code interpreter, as I understand the
early implementations were, but these days most of them do that
JIT compiling to native code, and while compiling isn't free,
it seems to me that if the cost of compiling a class is amortized
over many calls to its methods ....

Well. I don't mean to incite a flame war here -- I use and like
both Java and C -- but .... I wouldn't disagree with "Java is
generally slower, sometimes by a lot", but "Java is generally 5
to 10 times slower" seems to me to be too strong a claim. But I
admit that my perception is based more on anecdote and hearsay
than careful research.
In my experience I have found that Java programs are about 2 to 4 times
slower than equivalent C programs. However their start-up time is _far_
slower compared to their C counterparts, which may be one reason why
many people think they are 5 to 10 times slower. This is of course due
to the overhead of loading the VM and all the necessary classes.

Also speed isn't the only consideration. Java programs consume much more
memory than C or C++ programs. Each Java program, no matter how
trivial, must run with it's own instance of a VM, so if you run several
Java programs simultaneously, you'll find that virtual memory
consumption reaches very high levels, though I'll admit that physical
memory consumed is not so much, though still significantly larger than
what roughly equivalent C/C++ programs would take up.

Jun 27 '08 #5

P: n/a
On Apr 13, 5:23 am, santosh <santosh....@gmail.comwrote:
blm...@myrealbox.com wrote:
In article <ftqv65$n5...@registered.motzarella.org>,
Richard <de...@gmail.comwrote:
blm...@myrealbox.com <blm...@myrealbox.comwrites:
In article <1pKdnUM3IrV-A53VRVny...@bt.com>,
Malcolm McLean <regniz...@btinternet.comwrote:
"Sanny" <softta...@hotmail.comwrote in message

news:9c**********************************@y18g2000 pre.googlegroups.com...
I have an app in Java. It works fine. Some people say Java works
as
fast as C. Is that true?
No. C will typically be about 5-10 times faster than the same
application written in Java, if the bottleneck is in code written
in those languages. That's an important point, because often the
bottleneck is the rasteriser on the grapj=hics card or something
similar, in which case C will have no advantage.
In my (admittedly limited, but more than zero) experience, C
is indeed faster, but not by anything like a factor of 5 to 10.
I'm curious to know what kinds of applications, on what systems,
exhibit such dramatic differences. I just did a quick check
using a toy numerical-integration example for a class I teach,
and the Java version took about 2.9 seconds, while the C version
took about 1.6 seconds. This is on a reasonably fast Intel-based
It all depends on the applications and on how much time is spent in
the library functions. It really doesn't take a rocket scientist to
realise that C, written by a competent programmer, will generally be
faster than Java.
Probably so (despite the occasional claims by Java advocates
about the magic that can be worked via JIT compiling). But -- a
factor of 5 or 10? That's entirely plausible if the Java runtime
system is strictly a byte-code interpreter, as I understand the
early implementations were, but these days most of them do that
JIT compiling to native code, and while compiling isn't free,
it seems to me that if the cost of compiling a class is amortized
over many calls to its methods ....
Well. I don't mean to incite a flame war here -- I use and like
both Java and C -- but .... I wouldn't disagree with "Java is
generally slower, sometimes by a lot", but "Java is generally 5
to 10 times slower" seems to me to be too strong a claim. But I
admit that my perception is based more on anecdote and hearsay
than careful research.

In my experience I have found that Java programs are about 2 to 4 times
slower than equivalent C programs. However their start-up time is _far_
slower compared to their C counterparts, which may be one reason why
many people think they are 5 to 10 times slower. This is of course due
to the overhead of loading the VM and all the necessary classes.

Also speed isn't the only consideration. Java programs consume much more
memory than C or C++ programs. Each Java program, no matter how
trivial, must run with it's own instance of a VM, so if you run several
Java programs simultaneously, you'll find that virtual memory
consumption reaches very high levels, though I'll admit that physical
memory consumed is not so much, though still significantly larger than
what roughly equivalent C/C++ programs would take up.
Yeah, but of course you can compile java programs to native executable
code with Jet, and produce exe's that run at pretty much the same
speed as C/C++ ones (maybe slightly slower on average), and which
don't need the JRE.

So, the normal byte-code/VM implementation of Java programs is slower,
especially the start-up-time, but the language itself isn't
necessarily (or at least, not by much).
Jun 27 '08 #6

P: n/a
In article <ft**********@registered.motzarella.org>,
santosh <sa*********@gmail.comwrote:
bl****@myrealbox.com wrote:
In article <ft**********@registered.motzarella.org>,
Richard <de***@gmail.comwrote:
[ snip ]
It all depends on the applications and on how much time is spent in
the library functions. It really doesn't take a rocket scientist to
realise that C, written by a competent programmer, will generally be
faster than Java.
Probably so (despite the occasional claims by Java advocates
about the magic that can be worked via JIT compiling). But -- a
factor of 5 or 10? That's entirely plausible if the Java runtime
system is strictly a byte-code interpreter, as I understand the
early implementations were, but these days most of them do that
JIT compiling to native code, and while compiling isn't free,
it seems to me that if the cost of compiling a class is amortized
over many calls to its methods ....

Well. I don't mean to incite a flame war here -- I use and like
both Java and C -- but .... I wouldn't disagree with "Java is
generally slower, sometimes by a lot", but "Java is generally 5
to 10 times slower" seems to me to be too strong a claim. But I
admit that my perception is based more on anecdote and hearsay
than careful research.

In my experience I have found that Java programs are about 2 to 4 times
slower than equivalent C programs. However their start-up time is _far_
slower compared to their C counterparts, which may be one reason why
many people think they are 5 to 10 times slower. This is of course due
to the overhead of loading the VM and all the necessary classes.
Start-up time is indeed a significant factor.
Also speed isn't the only consideration. Java programs consume much more
memory than C or C++ programs. Each Java program, no matter how
trivial, must run with it's own instance of a VM, so if you run several
Java programs simultaneously, you'll find that virtual memory
consumption reaches very high levels, though I'll admit that physical
memory consumed is not so much, though still significantly larger than
what roughly equivalent C/C++ programs would take up.
Yes .... It seems to me, though, that it would be feasible for a
JVM implementation to make use of something like shared libraries
in the UNIX world, or DLLs in the Windows world, to reduce memory
use. Whether any actually do -- I guess this group isn't really
the place to ask, is it?

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Jun 27 '08 #7

P: n/a
In article <f5**********************************@u12g2000prd. googlegroups.com>,
Robert S <ro*********@gmail.comwrote:
On Apr 12, 9:01 pm, blm...@myrealbox.com <blm...@myrealbox.comwrote:
In article <aMidnZ9wgNOpkZzVnZ2dneKdnZydn...@bt.com>,

Malcolm McLean <regniz...@btinternet.comwrote:
[ snip ]
In my (admittedly limited, but more than zero) experience, C
is indeed faster, but not by anything like a factor of 5 to 10.
[ snip ]
Try a routine that uses mutli-dimensional arrays, and calculates numbers
based on adjacent elements in each direction.
What you'll find is that the Java indexing rules are nothing like as
efficient, and you'll get quite a considerable slowdown. I must admit I
haven't done my own timings, this is based on hearsay.
I also am not quite interested enough to do my own experiments, at
least right now. Certainly it's plausible that multidimensional
array access isn't as fast, given that conceptually a Java
multidimensional array is an array of array objects.

Time spent accessing 3D arrays on a modest wintel laptop:

C: 301 ms, Java: 731 ms, Size: 250*250*250
C: 171 ms, Java: 430 ms, Size: 200*200*200
I ran your programs (size 250 only) on a modest Linux desktop and
got -- well, actually they're *not* similar results, but a difference
more in line with the original claim (5 to 10 times slower):

C: 128 ms
Java: 635 ms

I noticed, though, that your Java code includes the allocation
of the array in the time being measured. If I time just the
part of the code that changes the array, I get:

Java, computation only: 121 ms

Whether it's "fair" (whatever that means in context) to include
allocating the array for the Java program but not the C program --
I'm not sure. But it does seem like the difference in overall
runtime isn't related to array access.

I am a bit curious, though, about why you made the array a
global variable in your C program. ?

(I tried making it a local variable in main() and -- as best
I could tell, gcc optimized it out of existence! which makes
a certain amount of sense, since the values placed in the array
are never used. Changing the program to instead use the array
values as input to some meaningless computation got around that,
but I wondered whether you had a similar experience, or your
reason for making it global was something else.)
Which fits in with what I've read about recent benchmarking: Java is
around about 2 to 3 times slower than C at most things.

Nowhere near 5 - 10 times.

Dev-cpp source:

#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>

#define size 250
int a[size][size][size];

int main(int argc, char *argv[])
{
struct timeb time1, time2;
time_t TimeMS; /*millisecond difference*/
time_t TimeS; /*time in seconds*/
time_t Elapsed_MS; /*elapsed time, milliseconds*/

ftime(&time1); /* starting time */

int i, j, k;

for(i=0; i<size; i++)
for(j=0; j<size; j++)
for(k=0; k<size; k++)
{
a[i][j][k] = i + j + k;
}

ftime(&time2); /* ending time */

TimeS = time2.time - time1.time; /*difference in seconds*/
TimeMS = time2.millitm - time1.millitm; /*millisecond
adjustment*/

Elapsed_MS = TimeS*1000 + TimeMS; /*elapsed time,
milliseconds*/

printf("%ld, %ld\n", time2.millitm, time1.millitm);
printf("Elapsed time in ms is: %ld\n", Elapsed_MS);
system("PAUSE");
return 0;
}

Java:

class testCvsJava
{
public static void main(String args[])
{
long m1;
long m2;
m1 = System.currentTimeMillis();

int size = 250;
int a[][][] = new int[size][size][size];
int i, j, k;

for(i=0; i<size; i++)
for(j=0; j<size; j++)
for(k=0; k<size; k++)
{
a[i][j][k] = i + j + k;
}

m2 = System.currentTimeMillis();

System.out.println("Execution time, ms: " + (m2 - m1));
}
}

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.