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

Discussion in 'Java' started by kaeli, Nov 18, 2004.

  1. kaeli

    kaeli Guest

    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
     
    kaeli, Nov 18, 2004
    #1
    1. Advertising

  2. Re: Which way is more efficient - comparing strings of differentletter casing

    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.
     
    Michael Borgwardt, Nov 18, 2004
    #2
    1. Advertising

  3. kaeli

    Chris Smith Guest

    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?


    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
     
    Chris Smith, Nov 18, 2004
    #3
  4. kaeli

    kaeli Guest

    In article <>,
    enlightened us with...
    > 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?

    >
    > 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
     
    kaeli, Nov 18, 2004
    #4
  5. kaeli

    kaeli Guest

    In article <>,
    enlightened us with...
    > 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?

    >
    > 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
     
    kaeli, Nov 18, 2004
    #5
  6. kaeli

    Chris Smith Guest

    kaeli <> 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
     
    Chris Smith, Nov 18, 2004
    #6
  7. kaeli

    Chris Smith Guest

    Chris Smith <> 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
     
    Chris Smith, Nov 18, 2004
    #7
  8. kaeli

    kaeli Guest

    In article <>,
    enlightened us with...
    > Chris Smith <> 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
     
    kaeli, Nov 18, 2004
    #8
  9. kaeli

    Chris Smith Guest

    kaeli <> 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/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
     
    Chris Smith, Nov 18, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grant Merwitz

    sessions, which is the more efficient way

    Grant Merwitz, May 11, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    390
    Grant Merwitz
    May 12, 2005
  2. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    759
    Reinhold Birkenfeld
    Jul 6, 2004
  3. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    507
    Tony Meyer
    Jul 6, 2004
  4. Andrew McNamara

    Re: big letter -> small letter

    Andrew McNamara, Jul 6, 2004, in forum: Python
    Replies:
    2
    Views:
    753
    Scott David Daniels
    Jul 6, 2004
  5. Kyle Hunter

    More efficient comparing

    Kyle Hunter, Feb 21, 2008, in forum: Ruby
    Replies:
    3
    Views:
    92
    Kyle Hunter
    Feb 21, 2008
Loading...

Share This Page