Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file2String faster code

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be faster file2String ?

this :


or :

Thank you!
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or this:


Thank you
Sharon
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried to profile any of them?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd be interested to hear where you got the code for the last example. It is almost correct, but actually quite crazy.

In general:
Buffered streams will be better.
StringBuffers will be better.
Reading into a buffer rather than per character will be better.
Readers and Writers will be better for character data.

You may also like to like at the NIO classes.

Are you trying to solve a problem, just playing, design a component or have some other aim?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
It is almost correct, but actually quite crazy.


I take that back, it is a variable with the name 'L', not a number 'one'. Calling it 'len' or 'length' would cause less confusion.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sharon whipple:
What will be faster file2String ?

this :


or :

Thank you!


These seem like hard work. How about
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Results :
test0 : Runs: 241 iterations ,avarage milis = 246
test1 : Runs: 249 iterations ,avarage milis = 118
test2 : Runs: 269 iterations ,avarage milis = 5
test3 : Runs: 241 iterations ,avarage milis = 2

TestClass :
 
Roger F. Gay
Ranch Hand
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. That's quite a difference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic