Solved

compare

Posted on 2004-04-28
10
348 Views
Last Modified: 2011-09-20
hi expert..
say:
int i = 500;
int j = 600;

String s1 = "Sad";
String s1 = "Bad";

if( i == j ) or if( s1.equals( s2) ) more faster???

please explan why?
0
Comment
Question by:Dishan Fernando
  • 4
  • 4
  • 2
10 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 25 total points
ID: 10937801
int will be faster - as it's a leaner operation. Why are you interested?
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 75 total points
ID: 10937806
primitive data type will be faster always.

i == j is a simple computation....

String is an object and there is a method invocation will cost bit more time

So always the primitive data type operations are faster than the object comparison.


Regards,
Muruga
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10937815
Hang on - they're actually likely to be virtually identical
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 7

Assisted Solution

by:searlas
searlas earned 25 total points
ID: 10937846
You're comparing an operator to a method invocation.  It doesn't matter that you're calling 'equals' on the string, it could be any method.

I'll go out on a limb here and say it's highly likely that ALL operators in Java are faster than method invocations.

i.e.  i == j, i << j, i + j, i * j, i / j.....

Faster than.
obj.method(param);

Method invocation implies stack manipulation, looking up method entry point etc etc...
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 75 total points
ID: 10937852
I am strongly saying that primitive data types are much faster than anything.  
Object are always slower than the primitive data type operations.


Here is the proof...

      public static void main(String[] args)
      {
            String a = "hello";
            String b = "whatis";
            
            int aa = 500;
            int bb = 600;
            
            long start = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++)
            {
                  if (aa == bb)
                  {
                  }
            }
            System.out.println("Integer comparison... time taken: " + (System.currentTimeMillis() - start));
            
            start = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++)
            {
                  if (a.equals(b))
                  {
                  }
            }
            System.out.println("String comparison... time taken: " + (System.currentTimeMillis() - start));
      }
0
 
LVL 7

Expert Comment

by:searlas
ID: 10937861
(before someone points it out... I'm not including str1 + obj as an operator as that implies calls to toString... therefore has implicit method invocation...)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10937874
I was thinking that the VM might have a way of knowing that a String taken from the constant pool is not the same as another one taken from the constant pool, but on inspection, it looks like this is not the case. The only real way to ascertain this sort of thing is by profiling it. 'Opinions' are not very useful
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937886
>> String b = "whatis";

String b = "whati";

with same length data with different string will increase the time taken...
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937901
that's great :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10937940
Along the lines of what i was getting at

         String a = "hello";
         String b = "hello";

will take quite a bit less time and

            String a = "haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            String b = "haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

won't be any slower
         
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 59
sql import cannot be resolved jsp 3 76
Java Restore security prompts not working 10 140
ejb entity bean example issue 2 27
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question