Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which is faster equals() or equalsIgnoreCase()

 
punna Kumar
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
could u plz explain the defference between the equals() and equalsIgnoreCase() methods in String class,and explain which is the better for performance.

Rgds,
punna
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Performance is not the major differentiation between the two methods, as they do different things. equalsIgnoreCase ignores the case of the two strings, just like its name suggests it should do.

"abc".equals("abc") --> true
"ABC".equals("abc") --> false
"def".equals("abc") --> false

"abc".equalsIgnoreCase("abc") --> true
"ABC".equalsIgnoreCase("abc") --> true
"def".equalsIgnoreCase("abc") --> false

If you're still interested in their relative performance, I would guess that equals is faster, because it only needs to compare the individual characters for equality, and not consider up-/downcasing it and then comparing that as well. That shouldn't make a big difference, though. The source of java.lang.String will tell you for sure.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be fully sure, you'd have to write a Benchmark. Writing a good one is an art, though. And it still wouldn't tell you wether it actually had any significant impact on the system you are developing - for that you needed to run it through a profiler.

"You don't need to care" is a quite good working hypothesis, though...
[ July 06, 2005: Message edited by: Ilja Preuss ]
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For most purposes the question is purely academic and could also change from one version of the jvm to another. For most purposes both are very fast. Plus, as another poster said they return different results, so you just can't swap them.

Here are some results I found using google on the matter. As, I recall Jack Shirazi's "Java Performance Tuning" book dicusses the matter too.

from http://www.javaperformancetuning.com

String.equalsIgnoreCase is a lot faster than String.equals when the majority of the compared data have different lengths because equalsIgnoreCase first checks the length of the two strings.




from http://java.sys-con.com/read/37555.htm

compareTo is two to three orders of magnitude faster than either of the equals methods. In my study - not shown here - I have found that the method call equalsIgnoreCase of the class String is a lot faster than the method call equals of the same class when the majority of the compared data have different lengths. There is a good reason for this, of course. The equalsIgnoreCase first checks the length of the two strings






[ July 08, 2005: Message edited by: steve souza ]
[ July 08, 2005: Message edited by: steve souza ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent point, Steve. Note, though, that (as you said!) these results can change. I just looked at the source for 1.4.2_08, and String.equals() does indeed check the lengths first, and equalsIgnoreCase() does a lot more work than equals() does, so it looks as though equals() is now the faster method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic