Java speed vs. C++.

M

Mike Cox

Hi. I recently ran a benchmark against two simple programs, one written in
Java and the other in C++. The both accomplish the same thing, outputting
"Hello World" on my screen. The C++ program took .5 seconds to complete on
my 400 Mhz PC while the Java program took 6.5 seconds.

I am running the SUSE 8.2 Linux distribution.

Why is Java that much slower than the C++ program? I read on Slashdot that
Java was almost as fast as C++. Here are my programs:

test.cpp

#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
}


test.java

public class test
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}

The reason I ask is because I'm thinking of using Apache and Jakarta to do
some development. If Java cannot be speeded up, I will be forced to find
another alternative.
 
M

Mike Cox

Siemel Naran wrote:

Maybe it takes time to start up the Java engine, so it will appear to be
much slower for a very short program. Try a lenghthier test. Also, the
Java implementation makes a difference too.

Would that be a concern when I use Jakarta to run a web application? Or
does Jakarta start up and never shut down the engine?
 
S

Siemel Naran

Mike Cox said:
Hi. I recently ran a benchmark against two simple programs, one written in
Java and the other in C++. The both accomplish the same thing, outputting
"Hello World" on my screen. The C++ program took .5 seconds to complete on
my 400 Mhz PC while the Java program took 6.5 seconds.

I am running the SUSE 8.2 Linux distribution.

Why is Java that much slower than the C++ program? I read on Slashdot that
Java was almost as fast as C++. Here are my programs:

test.cpp

#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
}


test.java

public class test
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}

The reason I ask is because I'm thinking of using Apache and Jakarta to do
some development. If Java cannot be speeded up, I will be forced to find
another alternative.

Maybe it takes time to start up the Java engine, so it will appear to be
much slower for a very short program. Try a lenghthier test. Also, the
Java implementation makes a difference too.
 
A

Ann

On my machine (WIN XP) it takes at least 5 seconds to start
any program from a dos window that is not built into COMMAND.COM
 
A

Ann

The reason I ask is because I'm thinking of using Apache and Jakarta to do
some development. If Java cannot be speeded up, I will be forced to find
another alternative.
Visual Basic is supported, I tried a "hello world" type program and it
took less than a second including the time to create the window.
 
E

E. Robert Tisdale

Mike said:
I recently ran a benchmark against two simple programs, one written in
Java and the other in C++. The both accomplish the same thing, outputting
"Hello World" on my screen. The C++ program took .5 seconds to complete on
my 400 Mhz PC while the Java program took 6.5 seconds.

I am running the SUSE 8.2 Linux distribution.
Why is Java that much slower than the C++ program?

Java programs are almost always almost as fast as C++ programs.
The problem with Java
(and other interpreted languages such as BASIC and MATLB)
is that it doesn't (can't) optimize certain [fine grain] operations
very well even if you compile the byte code into machine code.
This is very well documented in numerical programming.
I used Google

http://www.google.com/

to search for

+"Java numerical programming"

and I found lots of stuff.

The bottom line is
You should *not* be afraid to write applications --
even high performance numerical applications --
in Java but be aware that you might run into difficulties
that you just can't work around without calling a routine
written in some other language such as C++.
I read on Slashdot that Java was almost [always] as fast as C++.
Here are my programs:
cat test.cpp
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Hello World" << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o test test.cpp
g++ --version g++ (GCC) 3.4.1
time ./test
Hello World
0.003u 0.005s 0:00.00 0.0% 0+0k 0+0io 0pf+0w
cat test.java
public class test {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
javac test.java
which javac /usr/java/j2sdk1.4.2_05/bin/javac
time java test
Hello world
0.271u 0.042s 0:00.32 96.8% 0+0k 0+0io 1pf+0w
cat /etc/fedora-release
Fedora Core release 2 (Tettnang)
 
A

Arijit

Ann said:
On my machine (WIN XP) it takes at least 5 seconds to start
any program from a dos window that is not built into COMMAND.COM

Off topic, but Win XP doesn't even have command.com :)
 
A

Ann

Arijit said:
Off topic, but Win XP doesn't even have command.com :)
--------- ya does ---------------
Volume in drive C has no label.
Volume Serial Number is 502A-848E

Directory of C:\WINDOWS\SYSTEM32

08/29/2002 04:00 AM 50,620 COMMAND.COM
1 File(s) 50,620 bytes
0 Dir(s) 16,377,466,880 bytes free
 
C

Chris Uppal

Mike said:
Would that be a concern when I use Jakarta to run a web application? Or
does Jakarta start up and never shut down the engine?

The startup time is only an issue for applications that either need to start
really fast or which only run for a short time (like your example). For any
reasonably long running program, the startup time is essentially irrelevant
(however irritating while debugging).

One thing to beware of, though, when considering Java in a server environment,
is that the startup time makes Java a very poor choice for writing pure CGI
programs. (But then I don't think CGI is such a wonderful idea anyway...)

Incidentally, the latest Sun JVM has some tweaks to reduce the minimum startup
time. I find them quite effective.

-- chris
 
K

KiLVaiDeN

Mike Cox said:
Hi. I recently ran a benchmark against two simple programs, one written
in
Java and the other in C++. The both accomplish the same thing, outputting
"Hello World" on my screen. The C++ program took .5 seconds to complete
on
my 400 Mhz PC while the Java program took 6.5 seconds.

I am running the SUSE 8.2 Linux distribution.

Why is Java that much slower than the C++ program? I read on Slashdot
that
Java was almost as fast as C++. Here are my programs:

test.cpp

#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
}


test.java

public class test
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}

The reason I ask is because I'm thinking of using Apache and Jakarta to do
some development. If Java cannot be speeded up, I will be forced to find
another alternative.

Java takes time to initialise things, like preloading the classes, and lot
of other things. It's also an interpreted language, and it will be always
slower than compiled programs, no matter what you do. There is
native-compilers available if it really is a problem, and through JNI, you
can run native compiled code, if the need of speed is relevant.

But knowing those things, once running, a Java program, or at least a
consequent Java program which can be used for benchmarking purposes, can
compare in speed with C++ ones. The Jvm has had several optimisations that
made it quite good performance-wise to interpret Java code.

K
 
S

Scott Lopez

Java has a slower startup time because it's compiling the byte code to
native code. 80% of Java code is ready to run, 20% is platform specific
that the JVM must complete compilation for.

During the first run, your seeing Java do this. As the program runs beyond
the initial launch, you'll notice that Java continues to speed up, this is
because the HotSpot JIT compiler begins inlining code to make it run faster.

I believe the speed of both C++ and Java are close. Java has a slower
startup time, shouldn't be used for heavy trigonometric calculations and
should not be used in real-time systems (the garbage collector reaks havoc).
If these three issues aren't a concern, then Java is a great choice.

It's interesting to note this arguement never ends, it's religious. Soooo,
consider this. Java has significantly more tools available that have well
defined interfaces (JDO, XML parsing, XSLT, JMS), most performance errors
are due to developer coding and Java offers tools such as OptimizeIt, JProbe
and Deep Diagnostics to find these issues. In my mind, because you have
readily available tools, Java decreases your time to market and because you
have tools that inspect code during operation (profile) you can remove the
performance problems. Thus Java has faster time-to-market and (in most
cases) better performance.


KiLVaiDeN said:
Mike Cox said:
Hi. I recently ran a benchmark against two simple programs, one written
in
Java and the other in C++. The both accomplish the same thing, outputting
"Hello World" on my screen. The C++ program took .5 seconds to complete
on
my 400 Mhz PC while the Java program took 6.5 seconds.

I am running the SUSE 8.2 Linux distribution.

Why is Java that much slower than the C++ program? I read on Slashdot
that
Java was almost as fast as C++. Here are my programs:

test.cpp

#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
}


test.java

public class test
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}

The reason I ask is because I'm thinking of using Apache and Jakarta to do
some development. If Java cannot be speeded up, I will be forced to find
another alternative.

Java takes time to initialise things, like preloading the classes, and lot
of other things. It's also an interpreted language, and it will be always
slower than compiled programs, no matter what you do. There is
native-compilers available if it really is a problem, and through JNI, you
can run native compiled code, if the need of speed is relevant.

But knowing those things, once running, a Java program, or at least a
consequent Java program which can be used for benchmarking purposes, can
compare in speed with C++ ones. The Jvm has had several optimisations that
made it quite good performance-wise to interpret Java code.

K
 
@

@(none)

Chris said:
Mike Cox wrote:

Jakarta starts the engine at start-up, and never shuts it down. I think
there is even a possibility to configure it to preload classes, so even
the first request can be relatively rapid.
The startup time is only an issue for applications that either need to start
really fast or which only run for a short time (like your example). For any
reasonably long running program, the startup time is essentially irrelevant
(however irritating while debugging).
One thing to beware of, though, when considering Java in a server environment,
is that the startup time makes Java a very poor choice for writing pure CGI
programs. (But then I don't think CGI is such a wonderful idea
anyway...)

Like everything, it has its advantages and its disadvantages. A
separate process is the ultimate sand-box, for example, and nothing, but
absolutely nothing, in the CGI can possibly crash your server.

For most uses, however, I agree with you.
Incidentally, the latest Sun JVM has some tweaks to reduce the minimum startup
time. I find them quite effective.

I seem to recall reading somewhere that there was a possibility of
having the JVM (with the standard jar-files) pre-loaded somehow, so that
start-up times would be equivalent of those for other languages. Don't
know the details, of course.
 
C

Casey Hawthorne

"Code Complete 2nd Edition"
by Steve McConnell, 2004
page 600

lists Java's execution time compared to C++ as 1.5 to 1, with the
disclaimer that for any particular piece of code, C++, Visual Basic,
C#, or Java might be twice as fast or half as fast as the other
languages.
 
C

Chris Smith

Mike Cox said:
Hi. I recently ran a benchmark against two simple programs, one written in
Java and the other in C++. The both accomplish the same thing, outputting
"Hello World" on my screen. The C++ program took .5 seconds to complete on
my 400 Mhz PC while the Java program took 6.5 seconds.

Neither of these times are even in the ballpark of what you should be
seeing. Do to the startup overhead that's been well-discussed in this
group, your Java application should be running in about half a second,
while your C++ application should take something on the order of 10 to
50 milliseconds (that's 0.01 to 0.05 seconds). If these apps are really
taking that long, then you've got some other serious problems that's
affecting BOTH your Java and C++ code.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Ann said:
--------- ya does ---------------

Yes, Windows XP does have a command.com. However, it is not used for
anything, and the code probably hasn't been maintained for years.
Making use of it yourself is very ill-advised.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Michael Borgwardt

KiLVaiDeN said:
Java takes time to initialise things, like preloading the classes, and lot
of other things. It's also an interpreted language, and it will be always
slower than compiled programs, no matter what you do.

This is absolute nonsense. Java hasn't been an "interpreted language" for
years, at least not on the normal VMs. The code is compiled to native
at runtime by the VM (just-in-time-compiler, JIT), which is of course another
startup delay (that's why nowadays the VMs do it only for methods that
are called frequently) but once it has happened, Java can in fact be
*faster* than compiled C because the JIT has more information and e.g. can
optimize for the actually present CPU.
 
M

Michael Borgwardt

Chris said:
Neither of these times are even in the ballpark of what you should be
seeing. Do to the startup overhead that's been well-discussed in this
group, your Java application should be running in about half a second,
while your C++ application should take something on the order of 10 to
50 milliseconds (that's 0.01 to 0.05 seconds). If these apps are really
taking that long, then you've got some other serious problems that's
affecting BOTH your Java and C++ code.

Could be simply the effect of too little RAM, with the JVM needing some
swapping.
 
G

greg

using console output as a test of performance will not reflect your
actual results creating web applications.

1) you should test both C++ and JAVA programs using socket listeners.
measure the performance from entry into main() and exit from main(). do
a thousand or so requests (a single printf is not a valid test. using a
thousand will average the performance over time) use an external
program to send the requests. do some processing on each request.
2) there are probably benchmarks on the internet already comparing c++
and java, save yourself some time and use Google to find them. i found
these quickly:
http://www.idiom.com/~zilla/Computer/javaCbenchmark.html,
http://www.javaworld.com/javaworld/jw-02-1998/jw-02-jperf_p.html
3) thousands of commercial websites run using tomcat/java as the basis
for their web apps with perfectly acceptable performance results,
running multiple websites from a single tomcat instance with a few
thousand hits per hour.
4) in the long run, you will find java web apps to be more easily
maintained than c++ apps because you can use open sourced MVC tools
like Struts to handle a lot of the work for you
5) when doing benchmarks, use profiling tools (gcc -g) to determine
where your time is spent. especially in complex programs.
hope this helps. best of luck to you in your website.

greg
 
G

greg

using console output as a test of performance will not reflect your
actual results creating web applications.

1) you should test both C++ and JAVA programs using socket listeners.
measure the performance from entry into main() and exit from main(). do
a thousand or so requests (a single printf is not a valid test. using a
thousand will average the performance over time) use an external
program to send the requests. do some processing on each request.
2) there are probably benchmarks on the internet already comparing c++
and java, save yourself some time and use Google to find them. i found
these quickly:
http://www.idiom.com/~zilla/Computer/javaCbenchmark.html,
http://www.javaworld.com/javaworld/jw-02-1998/jw-02-jperf_p.html
3) thousands of commercial websites run using tomcat/java as the basis
for their web apps with perfectly acceptable performance results,
running multiple websites from a single tomcat instance with a few
thousand hits per hour.
4) in the long run, you will find java web apps to be more easily
maintained than c++ apps because you can use open sourced MVC tools
like Struts to handle a lot of the work for you
5) when doing benchmarks, use profiling tools (gcc -g) to determine
where your time is spent. especially in complex programs.
hope this helps. best of luck to you in your website.

greg
 
G

greg

using console output as a test of performance will not reflect your
actual results creating web applications.

1) you should test both C++ and JAVA programs using socket listeners.
measure the performance from entry into main() and exit from main(). do
a thousand or so requests (a single printf is not a valid test. using a
thousand will average the performance over time) use an external
program to send the requests. do some processing on each request.
2) there are probably benchmarks on the internet already comparing c++
and java, save yourself some time and use Google to find them. i found
these quickly:
http://www.idiom.com/~zilla/Computer/javaCbenchmark.html,
http://www.javaworld.com/javaworld/jw-02-1998/jw-02-jperf_p.html
3) thousands of commercial websites run using tomcat/java as the basis
for their web apps with perfectly acceptable performance results,
running multiple websites from a single tomcat instance with a few
thousand hits per hour.
4) in the long run, you will find java web apps to be more easily
maintained than c++ apps because you can use open sourced MVC tools
like Struts to handle a lot of the work for you
5) when doing benchmarks, use profiling tools (gcc -g) to determine
where your time is spent. especially in complex programs.
hope this helps. best of luck to you in your website.

greg
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top