8

Following setup:

int a=3;
String b="3";

Both variables represent IDs which are semantically equal. Since the application is for the mobile device, it is very important that the comparison of these variables is done in the most efficient way possible.

Is it efficient to compare these variables with this snippet,

boolean areEqual = Integer.parseInt(b) == a;

or with this one?

boolean areEqual = String.valueOf(a).equals(b);
sealz
  • 5,348
  • 5
  • 40
  • 70
maksuti
  • 129
  • 7
  • 5
    Just because it's mobile doesn't mean you need to do *everything* in the most efficient way possible. Have you performed any analysis to check that this is actually a bottleneck? If so, have you tried just doing it each way and comparing the results? Is there any possibility that the text value will *not* be a text representation of an integer? – Jon Skeet Aug 16 '13 at 16:58
  • 2
    You are unlikely to find any meaningful difference between the two. – Brian Roach Aug 16 '13 at 16:59
  • What happen if `b` is not an integer? – nachokk Aug 16 '13 at 16:59
  • Possible duplicate of http://stackoverflow.com/questions/508665/difference-between-parseint-and-valueof-in-java – Phil Aug 16 '13 at 17:02
  • 1
    http://stackoverflow.com/questions/15984623/java-comparing-ints-and-strings-performance – Pavlos Aug 16 '13 at 17:04
  • 6
    Premature optimization. Anyway the gain of speed is negligible unles this operation is performed millions of times. – m0skit0 Aug 16 '13 at 17:09

3 Answers3

4

It probably won't matter unless you're doing this comparison many thousands of times. That said, if you look at what each of those statements is doing:

boolean areEqual = Integer.parseInt(b) == a; This statement parses the String value once, then does an extremely fast comparison of two primitive int values.

boolean areEqual = String.valueOf(a).equals(b); This statement processes a String once to create the String value of a, and then does a String comparison. More steps, more internal logic, therefore less efficient.

StormeHawke
  • 5,987
  • 5
  • 45
  • 73
  • 1
    @Ruchira wrote a test, which I rewrote. This test supports your statements: http://ideone.com/wJmy3a – Martijn Courteaux Aug 16 '13 at 17:59
  • 1
    I didn't really look for confirmation tests... I just figured it's logical that fewer processor cycles per operation = higher efficiency. Granted sometimes something that looks like it should be simpler turns out not to be, but in this case it seemed pretty clear – StormeHawke Aug 16 '13 at 18:02
  • 1
    Yes, this was indeed pretty clear. I just tried to improve his false test (it gave opposite results). And it is a nice-to-see. – Martijn Courteaux Aug 16 '13 at 18:12
  • I think the second will indeed be slower, but mainly because a new `String` object has to be allocated (and later garbage collected). The first is probably done without any allocations. – Thomas Aug 16 '13 at 18:55
  • 1
    That's part of the problem as well. But string comparisons even with existing strings will always be inherently slower than int comparisons due to the need to compare each individual character in the string. An int comparison can be done in a single operation in the CPU – StormeHawke Aug 16 '13 at 18:57
1

The highest inefficiency of your code is probably that you convert between int and String at every single comparison. Just do the conversion from String to int right when you get the data at first place. This way you also ensure that any error message reaches your user immediately, e.g. when he/she mistyped the input.

jboi
  • 11,324
  • 4
  • 36
  • 43
1

If you do know that the string contains a digit, the most efficient way is probably;

boolean areEqual = (a == (b.charAt(0) - '0'));
assylias
  • 321,522
  • 82
  • 660
  • 783