Solved

compare

Posted on 2004-04-28
10
346 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 194
maven project error 5 55
more than one jdk and one jre 1 41
MySQL  on Tomcat 8 45
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 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