473,767 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.prin tln("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
30 2818
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*******@CaRa MaiL.CoM> wrote in message
news:41******** *************** @news.free.fr.. .

"Mike Cox" <mi**********@y ahoo.com> wrote in message
news:31******** *****@individua l.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.prin tln("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 objektorientier ter Datenverarbeitu ng
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**********@y ahoo.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.in valid> 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 "interprete d 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

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

Similar topics

73
8069
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 as a standard portable part of the core language, the LISP package, and the java.lang package, respectively. Both have big integers, although only LISP has rationals as far as I can tell. Because CL supports keyword arguments, it has a wider range...
6
5950
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 is this... is Perl so much better at parsing text files and outputing that we would see a substantial speed increase? We process about 10 million records in flat files a day for reformatting before putting them in a DB. Also, when it comes to...
4
5305
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 speed. Java and .NET seem to be the only two application platforms offering both, any suggestions? Thanks all!!
114
9880
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
9269
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 C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
19
3202
by: Davey | last post by:
Which is typically faster - a Java server application or a C++ server application?
6
13576
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 from char*/string to a numeric representation. Thanks!
29
2350
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 move on two either Java or C++, but I'm not sure which. Which one do you think is a softer transition for a Python programmer? Which one do you think will educate me the best?
7
3660
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. Can C++ directly acess the Registers. Will the Computation 5-10 times faster than Java?
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.