473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Java speed vs. C++.

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.


Jul 22 '05 #1
30 2747
"Mike Cox" <mi**********@yahoo.com> wrote in message
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.
Jul 22 '05 #2
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?
Jul 22 '05 #3
Ann

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:X4*******************@bgtnsc05-news.ops.worldnet.att.net...
"Mike Cox" <mi**********@yahoo.com> wrote in message
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:


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
Jul 22 '05 #4
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.
Jul 22 '05 #5
Ann wrote:

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 :)
Jul 22 '05 #6
Ann

"Arijit" <pa*****@yahoo.co.in> wrote in message
news:31*************@individual.net...
Ann wrote:

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 :)

--------- 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
Jul 22 '05 #7
Mike Cox wrote:
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)
Jul 22 '05 #8
Mike Cox wrote:
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
Jul 22 '05 #9

"Mike Cox" <mi**********@yahoo.com> wrote in message
news:31*************@individual.net...
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
Jul 22 '05 #10
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" <Ki*******@CaRaMaiL.CoM> wrote in message
news:41***********************@news.free.fr...

"Mike Cox" <mi**********@yahoo.com> wrote in message
news:31*************@individual.net...
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

Jul 22 '05 #11
Chris Uppal wrote:
Mike Cox wrote:
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?


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.

--
James Kanze home: www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Jul 22 '05 #12
"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.

--
Regards,
Casey
Jul 22 '05 #13
Mike Cox <mi**********@yahoo.com> wrote:
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
Jul 22 '05 #14
Ann <An**@nospam.invalid> wrote:
Off topic, but Win XP doesn't even have command.com :)

--------- 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
Jul 22 '05 #15
KiLVaiDeN wrote:
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.
Jul 22 '05 #16
Chris Smith wrote:
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.


Could be simply the effect of too little RAM, with the JVM needing some
swapping.
Jul 22 '05 #17
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...benchmark.html,
http://www.javaworld.com/javaworld/j...2-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

Jul 22 '05 #18
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...benchmark.html,
http://www.javaworld.com/javaworld/j...2-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

Jul 22 '05 #19
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...benchmark.html,
http://www.javaworld.com/javaworld/j...2-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

Jul 22 '05 #20
Chris Smith wrote:
Ann <An**@nospam.invalid> wrote:
Off topic, but Win XP doesn't even have command.com :)


--------- 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.


Since we're already way OT - the WinXP command shell (the so-called "DOS
prompt") is called CMD.EXE. It looks a lot like COMMAND.COM, but it's not.

--
Mike Smith
Jul 22 '05 #21
Mike Smith wrote:
Chris Smith wrote:
Ann <An**@nospam.invalid> wrote:
Off topic, but Win XP doesn't even have command.com :)
--------- 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.

Since we're already way OT - the WinXP command shell (the so-called "DOS
prompt") is called CMD.EXE. It looks a lot like COMMAND.COM, but it's not.

--
Mike Smith


Yeah, but the best WinXP command shell is called BASH.EXE, and you get
it from cygwin.
Jul 22 '05 #22

"Mike Cox" <mi**********@yahoo.com> wrote in message
news:31*************@individual.net...
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.

Mike,

This is a truly pathetic test. I am not sure you could learn ANYTHING of
value from it.

- dan elliott
Jul 22 '05 #23
Dan Elliott <da************************@nospam.org> scribbled the following
on comp.lang.java.programmer:
"Mike Cox" <mi**********@yahoo.com> wrote in message
news:31*************@individual.net...
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.

Mike,

This is a truly pathetic test. I am not sure you could learn ANYTHING of
value from it.


I agree. The overhead of starting the Java process and creating a VM is
way too large in this test. Try to print "Hello world" one million times
in a loop for a fairer test.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Jul 22 '05 #24

Mike Cox wrote:
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.


first explain how your super contrived hello world example has any
RELEVANCE what so ever to Apache, Jakarta or anything thing else server
related?

If you are not a troll, then your least worry is the speed of running a
contrived hello world example, it is more like getting much more basic
fundemental understanding about practical programming.
if you are just trolling just stop.

Jul 22 '05 #25
Mike Cox wrote:
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.


first explain how your super contrived hello world example has any
RELEVANCE what so ever to Apache, Jakarta or anything thing else server
related?

If you are not a troll, then your least worry is the speed of running a
contrived hello world example, it is more like getting much more basic
fundemental understanding about practical programming.
if you are just trolling just stop.

Jul 22 '05 #26
Mike Cox wrote:
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.


first explain how your super contrived hello world example has any
RELEVANCE what so ever to Apache, Jakarta or anything thing else server
related?

If you are not a troll, then your least worry is the speed of running a
contrived hello world example, it is more like getting much more basic
fundemental understanding about practical programming.
if you are just trolling just stop.

Jul 22 '05 #27
Mike Cox wrote:
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.


first explain how your super contrived hello world example has any
RELEVANCE what so ever to Apache, Jakarta or anything thing else server
related?

If you are not a troll, then your least worry is the speed of running a
contrived hello world example, it is more like getting much more basic
fundemental understanding about practical programming.
if you are just trolling just stop.

Jul 22 '05 #28
Mike Cox wrote:
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.


first explain how your super contrived hello world example has any
RELEVANCE what so ever to Apache, Jakarta or anything thing else server
related?

If you are not a troll, then your least worry is the speed of running a
contrived hello world example, it is more like getting much more basic
fundemental understanding about practical programming.
if you are just trolling just stop.

Jul 22 '05 #29
ja*************@yahoo.com wrote:
first explain how your super contrived hello world example has any
RELEVANCE what so ever to Apache, Jakarta or anything thing else server
related?
Actually, if he is counting on using Java to implement CGI scripts, he's
found one of the few application areas where it is relevant. (Of
course, one could question the reasonableness of such a choice, given
the presence of JSP.)
If you are not a troll, then your least worry is the speed of running a
contrived hello world example, it is more like getting much more basic
fundemental understanding about practical programming.
if you are just trolling just stop.


There are applications where start-up time is important. I have a
couple of small programs that I invoke from the editor to filter part of
the text -- typically just a couple of lines; JDK's start up time pretty
much makes Java a no runner here. There are other applications where it
isn't, but other things in Java implementations make the language
inappropriate. And of course, there are other applications for which it
is really the only reasonable alternative -- I'm certainly not going to
write CGI code in *any* language if I can use JSP.

For most large scale applications, of course, the performance of the
language simply isn't an issue these days.

--
James Kanze home: www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Jul 22 '05 #30
Joona I Palaste wrote:
Dan Elliott <da************************@nospam.org> scribbled the following
on comp.lang.java.programmer:
"Mike Cox" <mi**********@yahoo.com> wrote in message
news:31*************@individual.net...
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.


Mike,


This is a truly pathetic test. I am not sure you could learn ANYTHING of
value from it.

I agree. The overhead of starting the Java process and creating a VM is
way too large in this test. Try to print "Hello world" one million times
in a loop for a fairer test.

Actually that wont't do either. Console has its limits, and outputting
anything at that rate makes everything slow down to console speed. My
brother 'proved' that perl is working at same speed as c that way.
Better (but also silly) is to do loop that outputs something, does
something without ouputting, disk usage and so on (like
incrementing/decrementing or modulo-ing variable) and does it many
times, then execute that loop many more times :D.
I mean sometning like that:

#include <iostream>
using namespace std;
int main()
{
for(i=0; i<many_times;i++)
{
cout<<"Hello World";
for(j=0;j<many_many_times;j++)
{
i++;
i--;
}
}
}

and you have to take many_many_times high enough to slow doen output to
maybe one "hello" a second, and many many_times to take program to
execute not less than i think 30 secs to reduce impact of preparing a
program.
Jul 22 '05 #31

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
6
by: John Smith | last post by:
Hello, I have a rather odd question. My company is an all java/oracle shop. We do everything is Java... no matter what it is... parsing of text files, messaging, gui you name it. My question...
4
by: Arich Chanachai | last post by:
What are the pros and cons of Java? How does Java measure up to .NET in terms of speed? How about in terms of depth of classes? I am looking for garbage collection while at the same looking for...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
19
by: Davey | last post by:
Which is typically faster - a Java server application or a C++ server application?
6
by: mearvk | last post by:
Does C++ or C have something roughly equivalent to this: http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html What I need is a way to quickly convert between decimal and binary and...
29
by: s0suk3 | last post by:
Hello, I was hoping to get some opinions on a subject. I've been programming Python for almost two years now. Recently I learned Perl, but frankly I'm not very comfortable with it. Now I want to...
7
by: Sanny | last post by:
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.