473,842 Members | 1,526 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 #1
30 2834
"Mike Cox" <mi**********@y ahoo.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.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.


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*********@RE MOVE.att.net> wrote in message
news:X4******** ***********@bgt nsc05-news.ops.worldn et.att.net...
"Mike Cox" <mi**********@y ahoo.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******** *****@individua l.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\SYST EM32

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.prin tln("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**********@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 #10

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

Similar topics

73
8108
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
5953
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
5309
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
9909
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
9294
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
3209
by: Davey | last post by:
Which is typically faster - a Java server application or a C++ server application?
6
13583
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
2361
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
3661
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
9875
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
9715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10945
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
10614
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...
1
10674
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10317
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...
1
7859
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
5697
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...
0
5886
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.