Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Lesson learned  (Read 1583 times)
0 Members and 1 Guest are viewing this topic.
Offline 20thCenturyBoy

Full Member
**

Posts: 118


Ooh my head hurts...


« on: 2004-08-05 01:59:53 »


String message, line;

for (;;) {
   line = in.readLine();
   message += line;
}


Time taken to download a 1500 line email: 13 secs
Oh dear. Java is so slow! (Reads API...) What's this StringBuffer all about then?


StringBuffer message;
String line;

for (;;) {
   line = in.readLine();
   message.append(line);
}


Time taken to download a 1500 line email: 50ms

Hurrah! Java is fast!

I wish all optimisations were as easy as this!

Offline blahblahblahh

JGO Kernel
*****

Posts: 4575


http://t-machine.org


« Reply #1 on: 2004-08-05 02:51:34 »

I thought the compiler now handled all string concatenation with SB's anyway? And in the trivial analysis of that code there would be no obvious need to through away the intermediate SB between calls. Do you get different behaviour with server compared to client?

malloc will be first against the wall when the revolution comes...
Offline pepe

Sr. Member
**

Posts: 402


Nothing unreal exists


« Reply #2 on: 2004-08-05 04:28:55 »

i'll bet that if you decompile
1  
2  
3  
4  
 for (;;) {
    line = in.readLine();
    message += line;
}

you'll see something like that:
1  
2  
3  
4  
5  
6  
7  
String message;
 for (;;) {
    line = in.readLine();
    StringBuffer sb = new StringBuffer( message );
    sb.append( line );
    message = sb.toSTring();
}

Home page: http://frederic.barachant.com
------------------------------------------------------
GoSub: java2D gamechmark http://frederic.barachant.com/GoSub/GoSub.jnlp
Games published by our own members! Go get 'em!
Offline swpalmer

JGO Kernel
*****

Posts: 3438
Medals: 4


Where's the Kaboom?


« Reply #3 on: 2004-08-05 08:54:27 »

I think Pepe is correct... all string concatenation is done with SBs, but there is no optimization to keep the same SB across multiple statements, which is essentially what the loop above is all about.

Offline oNyx

JGO Kernel
*****

Posts: 2943
Medals: 5


pixels! :x


« Reply #4 on: 2004-08-05 09:30:17 »

Indeed. For that reason findbugs has a pattern for detecting that kind of stuff.

弾幕 ☆ @mahonnaiseblog
Offline 20thCenturyBoy

Full Member
**

Posts: 118


Ooh my head hurts...


« Reply #5 on: 2004-08-05 10:07:50 »

Findbugs?  <does Google....>
Ok, that'll be useful then  Grin
Offline swpalmer

JGO Kernel
*****

Posts: 3438
Medals: 4


Where's the Kaboom?


« Reply #6 on: 2004-08-05 10:58:58 »

Quote
Findbugs?  <does Google....>
Ok, that'll be useful then  Grin


If you were going to post that you googled you might as well have posted the link so I didn't have to take my hand off the mouse Smiley

http://findbugs.sourceforge.net/

Offline 20thCenturyBoy

Full Member
**

Posts: 118


Ooh my head hurts...


« Reply #7 on: 2004-08-05 21:04:54 »

Quote
If you were going to post that you googled you might as well have posted the link so I didn't have to take my hand off the mouse

Sorry!

I really must check out some more Java stuff on Sourceforge, I'll bet there is a ton of stuff that I'm missing out on.
Offline swpalmer

JGO Kernel
*****

Posts: 3438
Medals: 4


Where's the Kaboom?


« Reply #8 on: 2004-08-05 22:24:30 »

Yeah.. I just installed the FindBugs plugin for Eclipse.  It found a few things in my project that needed fixing.

Offline aldacron

Full Member
**

Posts: 200


Java games rock!


« Reply #9 on: 2004-08-05 23:00:10 »

Quote

Time taken to download a 1500 line email: 13 secs
Oh dear. Java is so slow! (Reads API...) What's this StringBuffer all about then?


Java 5.0 has a StringBuilder class which is the same as StringBuffer, but isn't synchronized.  So when you start using 5.0 perhaps you'll want to swtich to the new class.
Games published by our own members! Go get 'em!
Offline abies

Sr. Member
**

Posts: 456



« Reply #10 on: 2004-08-06 02:27:45 »

I have done some experiments with StringBuilder under server hotspot and I was not able to create any microbenchmark where it was noticeably faster. For me, it looks like hotspot is able to determine that this object is local and does not need synchronization ?? Situation will probably look different if you will pass StringBuffer/StringBuilder between methods.

Artur Biesiadowski
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.137 seconds with 20 queries.