Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Impact of 'instanceof' in Android Java code

Does the instanceof keyword bear with it a relatively heavier impact on the Android platform (and more speciffically the mobile phones running the Dalvik VM?

like image 579
Michiel Avatar asked Feb 13 '10 23:02

Michiel


People also ask

Is it good to use Instanceof in Java?

Probably most of you have already heard that using “instanceof” is a code smell and it is considered as a bad practice. While there is nothing wrong in it and may be required at certain times, but the good design would avoid having to use this keyword.

What is the purpose of Instanceof operator in Java?

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .

What is the advantage of using Instanceof?

We can use instanceof operator to check whether an object reference belongs to parent class, child class, or an interface. It's also known as type comparison operator because it compares the instance with type.

Is Instanceof fast Java?

Instanceof is very fast. It boils down to a bytecode that is used for class reference comparison. Try a few million instanceofs in a loop and see for yourself.


2 Answers

I do not think instanceof bears a heavier impact on the Dalvik VM opposed to the JVM.

If you have any doubts, you can see for yourself when you run your application with a tool called Allocation Tracker that comes standard as a tool for the DDMS.

like image 160
Anthony Forloney Avatar answered Oct 10 '22 18:10

Anthony Forloney


I found that instanceof is mostly faster (around 60-85% of the time). However this percentage falls when the phone is presented with background activity (e.g. GC, touching, buttons, shaking it etc.) but instanceof remains faster above 50% of the time. When the number of cycles is made very large (i.e. > 1000000) instanceof is nearly always faster. The order in which the two while loops are presented (i.e. first the instanceof loop and then the field check loop) affects the results but instanceof remains the fastest.

        AbstractParticle circleParticle = new CircleParticle();
        int cycles = 100000

        long now1 = System.currentTimeMillis();
        int i = 0;
        while(i<cycles) {
            if(circleParticle instanceof CircleParticle) {
                i++;
            }
        }
        long timetaken1 = (System.currentTimeMillis()-now1);

        long now2 = System.currentTimeMillis();
        int j = 0;
        while(j<cycles) {
            if(circleParticle.type == AbstractParticle.TYPE_CIRCLE) {
                j++;
            }
        }
        long timetaken2 = (System.currentTimeMillis()-now2);

        if(timetaken1 < timetaken2) {
            type1won++;
        }
        else if(timetaken2 < timetaken1){
            type2won++;
        }

        Log.d("instanceof test","type 1 favoured : "+((float)type1won/(type1won+type2won)));        
like image 22
Michiel Avatar answered Oct 10 '22 16:10

Michiel