aspose file tools*
The moose likes Java in General and the fly likes Code Snippet performance and quality. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Code Snippet performance and quality." Watch "Code Snippet performance and quality." New topic
Author

Code Snippet performance and quality.

Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

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.



Time is what we want the most, but what we use the worst. -- William Penn
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7784
    
  21

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



Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
You say that thread safety is a concern, so you should use StringBuffer, not StringBuilder.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

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.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code Snippet performance and quality.