Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code Snippet performance and quality.

 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Thread safety, performance, quality, complexity - which one of the below snippet is better based on these ?. May be 7.5 million transactions per 5 minutes.

 
Tim Moores
Bartender
Posts: 2798
38
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The latter depends on assumptions about the internal workings of the List.toString method - those are not documented and could change from release to release. Thus, you should not rely on it.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim is right. However, should you choose to rely on it, using replaceAll is a bad idea. That will not only remove the first and last [], but also any occurrence within the String. Better is to just cut off the first and last character, which will also be faster:
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:The latter depends on assumptions about the internal workings of the List.toString method - those are not documented and could change from release to release. Thus, you should not rely on it.

Actually, they are - in AbstractCollection - which pretty much any List (including the one produced by Arrays.asList()) will implement.

That said, I totally agree with your general statement.

@Robin: Personally, I'd pick option 1, for the simple reason that you don't want the format produced by Arrays.asList().toString(). I'd also be very surprised if the difference in time is noticeable.

Winston


 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say that thread safety is a concern, so you should use StringBuffer, not StringBuilder.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:You say that thread safety is a concern, so you should use StringBuffer, not StringBuilder.


Well, assuming that this piece of code would be in a method, then it wouldn't be necessary.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic