Why Java is so slow?

G

Guest

I have about 2600 lines of this type inside a text file:
----------------------
20/11/1980/My birth!
31/12/2003/A day before this year.
30/07/2004/A Boring Day!
etc
----------------------

These lines describe historical anniversaries.
I have a web page (PHP) with this theme, and I want to create a Java
application for this (stand-alone).

The problem is this:
PHP wants 0,3 seconds to create an HTML with ALL these lines (all of
2600!!!)
BUT in the same machine Java wants 2 minutes!!!!

So, Why Java is so SLOOOOOW?

I see that: Java adds to output String, first 240 lines in about 3 seconds.
I believe there is a problem with reallocation of String.

Because the String (in worst case) is about 800 KBs long, Can I set an
initial size for String?
 
M

Michael Borgwardt

So, Why Java is so SLOOOOOW?

It's not Java that is slow, it's your code!
I see that: Java adds to output String, first 240 lines in about 3 seconds.
I believe there is a problem with reallocation of String.

Ah, you've probably identified location of the problem, code like this:

outputString += "<b>"+name+"</b>";
outputString += "<i>"+date+"</i>";

This is very bad, but the problem is not with "reallocation", because
this concept does not exist in Java. What the code does is to create
a new String object each line by copying the previous value of outputString
and adding the new parts, but the old String is discarded and left to
the garbage collector to clean up.

A very typical beginners' mistake. There likely are other problems, but
we can't tell without seeing your code.
Can I set an initial size for String?

Don't assemble your entire output at all! Take the Writer or Stream you're
ultimately going to output the data to, and write each part of the
output to it as it becomes available.
 
M

Mike Schilling

I have about 2600 lines of this type inside a text file:
----------------------
20/11/1980/My birth!
31/12/2003/A day before this year.
30/07/2004/A Boring Day!
etc
----------------------

These lines describe historical anniversaries.
I have a web page (PHP) with this theme, and I want to create a Java
application for this (stand-alone).

The problem is this:
PHP wants 0,3 seconds to create an HTML with ALL these lines (all of
2600!!!)
BUT in the same machine Java wants 2 minutes!!!!

So, Why Java is so SLOOOOOW?

I see that: Java adds to output String, first 240 lines in about 3 seconds.
I believe there is a problem with reallocation of String.

Because the String (in worst case) is about 800 KBs long, Can I set an
initial size for String?

You don't want to concatenate Strings. Assemble the characters in a
StringBuffer and convert that to a string when you're done. You can give a
StringBuffer an initial size.
 
F

Flip

outputString += said:
outputString += "<i>"+date+"</i>";
Another solution to this is to use StringBuffer if you do indeed need to
concatenate the strings together.

StringBuffer outputString = new StringBuffer(); // or use StringBuffer
outputString = new StringBuffer( fileSize );
for( int i = 0; i < fileSize; i++ ){
outputString.append( "<b>" + name + "</b>" );
outputString.append( "<i>" + date+ "</i>" );
}

Ok, Ok, I rhimed this off the top of my head and it might not exactly
compile, but I do hope you can see where I'm going with this. I do did the
old + for String concatenation and learned the StringBuffer is a much better
way.

Only one caveat with the StringBuffer. You have to remember to use
toString() when you want to get the String out of it. No big deal, just
something that still hoops me sometimes.

Good luck.

PS re Java is slow?
Academically, yes, in comparison to C++ it is slower. But that is at a very
theoretical level. The same can be said with comparing C++ to C (OOP just
has an overhead). And can you imagine the speed difference between C and
Assembly? Mind you I would HATE to have to code in assembly! I never got
in to device driver development cause of that.

When you come up against a performance issue, the first culprit is the
algorithm or objects being used. Coding an insertion sort in C and
comparing it to a quicksort in Java will have widely different results over
a large sample, Java being the winner I would guess. :> So when in doubt,
check the algorithm before the language. I don't doubt there are times when
you need to make the conclusion the lanuage is the culprit, but for what
most people do, it's the algorithm.

PPS There might be a better approach to the above solution instead of the
StringBuffer, but just in case you need the string, that StringBuffer class
is invaluable! But as I mentioned if even that solution is too slow, try a
different algorithm, I believe Michael made a great suggestion for this.
Good luck.
 
G

Guest

You don't want to concatenate Strings. Assemble the characters in a
StringBuffer and convert that to a string when you're done. You can give a
StringBuffer an initial size.


Sorry, but I am wrong!
I create String from a StringBuffer and the needed time is 0.1 second!

The waste of time is here:

JEditorPane txtHistory = new JEditorPane("text/html", null);
................
// s is a String with 603119 characters
txtHistory.setText(s);

What I can do?
 
G

Grant Wagner

Flip said:
Another solution to this is to use StringBuffer if you do indeed need to
concatenate the strings together.

StringBuffer outputString = new StringBuffer(); // or use StringBuffer
outputString = new StringBuffer( fileSize );
for( int i = 0; i < fileSize; i++ ){
outputString.append( "<b>" + name + "</b>" );

I think you want:

outputString.append("<b>").append(name).append("</b>");

I'd be a bit afraid the code you showed is doing:

outputString.append(new
StringBuffer().append("<b>").append(name).append("</b>").toString());

Although if it's optimizing the toString() away, then I suppose:

outputString.append(new
StringBuffer().append("<b>").append(name).append("</b>"));

wouldn't be so bad. Of course, you're still creating a new StringBuffer() just
so you can append it to another one.

Of course, as someone else mentioned, reading the entire file into memory (even
as a StringBuffer) is unnecessary and a waste of resources.
 
M

Mike Schilling

give


Sorry, but I am wrong!
I create String from a StringBuffer and the needed time is 0.1 second!

The waste of time is here:

JEditorPane txtHistory = new JEditorPane("text/html", null);
...............
// s is a String with 603119 characters
txtHistory.setText(s);

What I can do?

There's an alternative listed in the Javadoc, which is to create a
StringReader and use JEditorPane .read() on it. Whether this is any faster
(i.e. if it's smart enough to page the string in from the reader rather than
suck it all in at once) I can't say.
 
C

Carl Howells

Sorry, but I am wrong!
I create String from a StringBuffer and the needed time is 0.1 second!

The waste of time is here:

JEditorPane txtHistory = new JEditorPane("text/html", null);
...............
// s is a String with 603119 characters
txtHistory.setText(s);

What I can do?

Stop using GUI components for a text-processing task?

What in the world made you decide JEditorPane was at all appropriate for
this? It's not.
 
R

Roedy Green

JEditorPane txtHistory = new JEditorPane("text/html", null);

What kind of program is this? A servlet, and Application, an Applet?

For a large list displayed to the user, JTable is normally what you
want. If you are generated HTML in a servlet to send to a browser, you
don't need a JEditorPane. Editorpane is for editing. I don't recall
you saying anything about editing.
 
R

Roedy Green

So, Why Java is so SLOOOOOW?

A 15 year old sitting in a Ferrarri in neutral, revving the engine,
asking the engineers why Ferraris are such crap. You have to learn to
drive first.
 
W

Will Hartung

Carl Howells said:
What in the world made you decide JEditorPane was at all appropriate for
this? It's not.

I dunno, perhaps for some bizarre reason he wants to display/edit the text.

The way I read this is that "his program was slow" and he thought that
creating the String was the "slow" part.

Later, he seems to recognize that his time sink is not creating the String,
but creating the JEditorPane.

This is all supposition, but that's the way I understand his posts so far. I
didn't read anything that suggested he was using the JEditorPane to build
his String for him.

I won't mention the detail that he seems to have inadvertantly noted that
Java was 3 times faster than PHP for the same task. ;-)

Regards,

Will Hartung
([email protected])
 
C

Chris Smith

Grant said:
I'd be a bit afraid the code you showed is doing:

outputString.append(new
StringBuffer().append("<b>").append(name).append("</b>").toString());

It probably is.

Nevertheless, either one solves the important problem. Quibbling over
constant factors in performance is something to be done with the aid of
a profiler, and the cost of building a StringBuffer and copying a
relatively short bit of text is not something I'd be overly worried
about.

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

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

Tony Morris

System.out.println(claim.isCorrect()); // false
claim = Claim.create(claim.replace("Java", "Java programmer"));
System.out.println(claim.isCorrect()); // true

A lot of Java programmers write slow code because they are misinformed or
under-informed. If you provide some sample source, you might be able to
become more informed.
 
G

Guest

JEditorPane txtHistory = new JEditorPane("text/html", null);
What kind of program is this? A servlet, and Application, an Applet?

my program is application.
For a large list displayed to the user, JTable is normally what you
want. If you are generated HTML in a servlet to send to a browser, you
don't need a JEditorPane. Editorpane is for editing. I don't recall
you saying anything about editing.


I dont care for editiing. I want view an HTML format String in a TextArea.
So, I choice JEditorPane and I set
editorpane.setEditable(false);

is there a better way?
 
A

Alex Zorin

It is slow because machine code can be executed directly, but java still
needs a Java interpreter to run stuff...
 
M

Michal Dzirba

Dnia 2004-08-01 12:49, U¿ytkownik Alex Zorin napisa³:
It is slow because machine code can be executed directly, but java still
needs a Java interpreter to run stuff...
Well, there is no way that PHP is using native code (without previus
precompilatoin, and we-made-it-look-likejit stuff),

Things that might have an influence on so called performance are the jvm
startup (which usually takes more time than the competiting vm's) and
the coding style. Also swing stuff seems havy (performace - wise) at
times. But in general, long running java programs can be written so that
to appear more efficient than programs written in competiting
technologies. Thanks to jit and stuff.


Michal.
 
T

Tor Iver Wilhelmsen

Alex Zorin said:
It is slow because machine code can be executed directly, but java still
needs a Java interpreter to run stuff...

Er, do you know what "just in time" compilation means? It means it's
translated into machine code at startup, and that's the code that
runs. Very little of Java runs interpreted; only when the interpreted
way is faster.
 
B

Bill Tschumy

Sorry, but I am wrong!
I create String from a StringBuffer and the needed time is 0.1 second!

The waste of time is here:

JEditorPane txtHistory = new JEditorPane("text/html", null);
...............
// s is a String with 603119 characters
txtHistory.setText(s);

What I can do?

I've noticed the same thing in an app I'm writing that uses JEditorPane to
display HTML. It is mainly slow the first time you set the text. Subsequent
times are much faster. Because of this I think the time sink is in
initialization of all the classes and fonts that are used for HTML display.
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top