Solved

compare

Posted on 2004-04-28
10
344 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

707 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

14 Experts available now in Live!

Get 1:1 Help Now