Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String size, which approach is faster?  RSS feed

 
Wagner Danda Da Silva Filho
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering that str is a String variable and is never null, which approach is faster?




[ November 01, 2005: Message edited by: Wagner Danda ]
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the equals method


Below is the length() method


the equals just executes



When the String to be compared is not empty, equals executes more number of steps than length.

Hence I think length() > 0 is more predictable and faster.
[ November 01, 2005: Message edited by: jiju ka ]
 
Scott Selikoff
author
Bartender
Posts: 4087
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer neither, sticking with:



Since this has constant running time and the empty string is defined to have a hashCode() of zero. Using "" is never too good because you have to allocate the space for the empty string.

I also throw in a trim() 99% of the time for strings using str.trim().hashCode(). Its not required and depends on your input.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:
I prefer neither, sticking with:




Ugh. I'll grant you that's an interesting observation, but it's obscure. On seeing this condition, the fact that we're testing for length 0 is not the first thing that pops to mind.

Why is this better than length() == 0 ?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:
[QB]I prefer neither, sticking with:



Since this has constant running time and the empty string is defined to have a hashCode() of zero.


It's not the only String that has a hashcode of zero, though. Therefore it's actually *wrong*!

Using "" is never too good because you have to allocate the space for the empty string.


An empty String doesn't take up much space, though - and once you have one, it's in the constant pool, so you can use it at other places without any further heap space being allocated.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wagner Danda:



This were better be written as

if(!str.equals(""))
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't better than length()==0:
returns zero.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All solutions perform acceptably from a performance standpoint. Pick the way that you feel most clearly models what you are trying to accomplish.

However, I prefer !"".equals(str) to !str.equals(""). The second can throw a null pointer exception if str is null. Alternatively you could do (str==null || str.equals(""), although I don't prefer this way.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's not overlook that Wagner stated on the very first line that str is never null. So for this particular question, null is a non-issue. However if we do wish to consider other versions of the problem in which the string may be null: my experience is that often the requirement ends of being that a null string and an empty string are considered as equivalent. Many applications to not enforce a meaningful distinction between null and empty strings, even if they probably should (or ban one of the two values as invalid). If you're working on such an application, then chances are that this test does not meet your needs:

because if str is blank, it's false, but if str is null, it's true. Instead something like this is probably preferable:

or alternately, its opposite:

Now which of these is correct depends on your specific requirements of course. But I would suggest that if you need to handle the case of null, chances are good that you need an expression which explicitly shows how null should be handled.

Now having said that, in general I would prefer to handle null by throwing a NullPointerException and notifying the calling code that they screwed up by providing my method with an invalid argument. Except in those rare cases where null really does signal something unique and different from "". My experience is that null is greatly overused in Java applications, and in general we'd be best to avoid it as much as possible. But if null must be tolerated as a legal argument, then I recommend explicitly handling it so it's easier to tell what's going on for anyone who reads the code.
 
JuanP barbancho
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manage Execption is more slower than do not manage.

Test the following


or



Thanks
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure you didn't test it yourself, because it will not compile:


Avoiding exceptions for performance-reasons sounds curious to me.
Would you remove the fire extinguisher from your car to gain more speed?

And I've learned, that entering a 'try'-Block is free.
Only in the case an exception is thrown, you spend some time.

To be sure about that, I measured your code with the result: no measurable differnce for 1,000,000,000 times entering the try-Block.

How did you measure a difference?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by JuanP barbancho:
Manage Execption is more slower than do not manage.


Jim's suggestion was to never use null as a parameter. Google for the Null Object pattern.
 
Scott Selikoff
author
Bartender
Posts: 4087
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It's not the only String that has a hashcode of zero, though. Therefore it's actually *wrong*!


Arg, this is what I get for reading the API where it says "The hash value of the empty string is zero." I took that statement to be "iff". Oh well.

An empty String doesn't take up much space, though - and once you have one, it's in the constant pool, so you can use it at other places without any further heap space being allocated.


I never said the performance increase was noticible, I meant more from a theory perspective, it adds additional cost.
 
Wagner Danda Da Silva Filho
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone! Regarding the many approaches detailed here, I'll use the one that matches better to my requirements:



Thanks again!

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!