Solved

compare

Posted on 2004-04-28
10
345 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:dishanf
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now