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

K

kaeli

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. :)

--
 
M

Michael Borgwardt

kaeli said:
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.
 
C

Chris Smith

kaeli said:
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
 
K

kaeli

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
 
C

Chris Smith

kaeli said:
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
 
C

Chris Smith

Chris Smith said:
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
 
K

kaeli

Chris Smith said:
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... ;)

--
 
C

Chris Smith

kaeli said:
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/AlgDesignManual/BOOK/BOOK3/NODE130.HTM
http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top