Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Which way is more efficient - comparing strings of different letter casing

Reply
Thread Tools

Which way is more efficient - comparing strings of different letter casing

 
 
kaeli
Guest
Posts: n/a
 
      11-18-2004

If I have a string and I don't know the letter case, and I wish to compare it
to a known string of known case, which of these methods is more efficient?

The string is (would be) in the variable "myStr" and we do not know if it is
null and we do not know the case of the letters.

A:
if ("VALUE".compareToIgnoreCase(myStr) == 0) {
// do something
}

B:
myStr = myStr==null?null:myStr.toUpperCase()
if ("VALUE".equals(myStr)) {
// do something
}

Even more efficient code than either of these two welcome.

--
--
~kaeli~
Local Area Network in Australia:... the LAN down under.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      11-18-2004
kaeli wrote:

> If I have a string and I don't know the letter case, and I wish to compare it
> to a known string of known case, which of these methods is more efficient?
>
> The string is (would be) in the variable "myStr" and we do not know if it is
> null and we do not know the case of the letters.
>
> A:
> if ("VALUE".compareToIgnoreCase(myStr) == 0) {
> // do something
> }
>
> B:
> myStr = myStr==null?null:myStr.toUpperCase()
> if ("VALUE".equals(myStr)) {
> // do something
> }


1. Why are you worrying about this? Has a profiler told you that it's this
comparison that your application spends most of its time in?

2. Use the source, Luke. You have access to the source code of the String class.

3. Looking at it will reveal that it's full of checks and optimizations for special
cases, which means that your question cannot be meaningfully answered - it depends
on the kind of strings you want to compare.

4. Why don't you just try it out? Writing a little benchmark will give you an answer
four your actual data, JVM and hardware, and is a matter of 5 minutes.
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      11-18-2004
kaeli <(E-Mail Removed)> wrote:
> If I have a string and I don't know the letter case, and I wish to compare it
> to a known string of known case, which of these methods is more efficient?


Using equalsIgnoreCase is the right way to do this. I can almost
guarantee it's more efficient, just based on simple logic -- if it
weren't, then Sun would replace the current implementation with your
code instead. The key point, though, is not that it's measured to be
efficient, but simply that it's the right abstraction.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
kaeli
Guest
Posts: n/a
 
      11-18-2004
In article <(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)
enlightened us with...
> kaeli <(E-Mail Removed)> wrote:
> > If I have a string and I don't know the letter case, and I wish to compare it
> > to a known string of known case, which of these methods is more efficient?

>
> Using equalsIgnoreCase is the right way to do this.


I feel like a moron. I didn't know about that method.
One would think I could read the API docs. Really.
*embarrassed grin*

--
--
~kaeli~
God was my co-pilot... but then we crashed in the mountains
and I had to eat him.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
kaeli
Guest
Posts: n/a
 
      11-18-2004
In article <(E-Mail Removed)>, (E-Mail Removed)
enlightened us with...
> kaeli <(E-Mail Removed)> wrote:
> > If I have a string and I don't know the letter case, and I wish to compare it
> > to a known string of known case, which of these methods is more efficient?

>
> Using equalsIgnoreCase is the right way to do this. I can almost
> guarantee it's more efficient, just based on simple logic -- if it
> weren't, then Sun would replace the current implementation with your
> code instead. The key point, though, is not that it's measured to be
> efficient, but simply that it's the right abstraction.
>
>


Now I'm wondering...
What are some valid uses for compareTo and compareToIgnoreCase, then? My
first thought was sorting, but there has to be a more efficient way to sort
than that.

--
--
~kaeli~
God was my co-pilot... but then we crashed in the mountains
and I had to eat him.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      11-18-2004
kaeli <(E-Mail Removed)> wrote:
> Now I'm wondering...
> What are some valid uses for compareTo and compareToIgnoreCase, then? My
> first thought was sorting, but there has to be a more efficient way to sort
> than that.


Sorting is the main one. Of course, the sort algorithm is provided by
Collections.sort and Arrays.sort; but the comparison is pluggable.
String implements the Comparable interface, so the compareTo method
actually defines the default ordering of String objects if you don't
specify a Comparator to the sort. compareToIgnoreCase, on the other
hand, would need to be specified in an explicit Comparator as follows:

Collections.sort(myStrings, new Comparator() {
public int compare(Object a, Object b)
{
return ((String) a).compareToIgnoreCase((String) b);
}
});

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      11-18-2004
Chris Smith <(E-Mail Removed)> wrote:
> Sorting is the main one. [...]


Let me also mention that these methods, along with Comparable and
Comparator can be used in other ways that relate to ordering of objects
but are not sorting per se. For example, many data structures implement
certain ordering constraints that can be checked via compareTo methods,
but stop short of full-fledged sorting. A heap tree used to implement a
priority queue, for example, would fit this description.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
kaeli
Guest
Posts: n/a
 
      11-18-2004
In article <(E-Mail Removed)>, (E-Mail Removed)
enlightened us with...
> Chris Smith <(E-Mail Removed)> wrote:
> > Sorting is the main one. [...]

>
> Let me also mention that these methods, along with Comparable and
> Comparator can be used in other ways that relate to ordering of objects
> but are not sorting per se. For example, many data structures implement
> certain ordering constraints that can be checked via compareTo methods,
> but stop short of full-fledged sorting. A heap tree used to implement a
> priority queue, for example, would fit this description.
>
>


Thanks for the info.

Can you point me to more resources about the heap tree / priority queue? I
currently have an application that deals with a queue for jobs and, having
coded it fresh and new to java from C, I coded it as vectors and do all kinds
of interesting things to try to figure out which job has priority...

--
--
~kaeli~
Acupuncture is a jab well done.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      11-18-2004
kaeli <(E-Mail Removed)> wrote:
> Thanks for the info.
>
> Can you point me to more resources about the heap tree / priority queue? I
> currently have an application that deals with a queue for jobs and, having
> coded it fresh and new to java from C, I coded it as vectors and do all kinds
> of interesting things to try to figure out which job has priority...


Any data structures book should discuss this. Here are some online URLs
that look good to get you started:

http://www2.toki.or.id/book/AlgDesig...K3/NODE130.HTM
http://ciips.ee.uwa.edu.au/~morris/Y...210/heaps.html
http://www.csua.berkeley.edu/~ranga/school/cs161/

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
More efficient comparing Kyle Hunter Ruby 3 02-21-2008 11:14 PM
sessions, which is the more efficient way Grant Merwitz ASP .Net 4 05-12-2005 06:13 PM
upper-casing parts of xpath Johannes Koch XML 3 09-18-2004 06:34 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM
Shockproof & Water proof casing for a S230? J Digital Photography 0 09-23-2003 03:30 AM



Advertisments